Check word variation in a sentence with Regexp

Asked

Viewed 534 times

1

I have a code that checks phrases that the user types and was tried more without much success, make a code using Regexp check that the user typed a particular phrase without or with word variations.

Example: "I use Facebook" or "I use Google"

The two sentences are almost identical with only one word variation.

I wanted this code to check if the user typed this phrase with possible predetermined variations.

I don’t understand much about it, but I think it would be more or less like this:

"Eu uso o google".exec( /^Eu uso o [google, facebook]$/i );

1 answer

2


The method .exec() is on the contrary, should be regex.exec(str), and in case you only want a boolean you can use the .test(). However you can take the $ and do so:

var str = "Eu uso o google";
var regex = /^Eu uso o [google, facebook]/i;
var teste = regex.test(str); // true

Example: http://jsfiddle.net/9nwC7/

A site I use to test regular expressions is: http://regex101.com/r/bS4nT8/1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.