JavaScript regex to find string -


i have string , 2 words dictionary , trying know if in string there words not dictionary words.

var string = 'foobarfooba';  var patt = new regexp("[^(foo|bar)]");// words dictionary var res = patt.test(string);   console.log(res);//return false 

it should return true because in string there 'ba', return false.

same phil commented in question, have rid of character class:

[^(foo|bar)] ^-- here --^ 

character classes used match (or not match if use ^) specific unsorted characters.

just use:

var patt = new regexp("(?:foo|bar)");// words dictionary 

if want ensure string matches regex, can use:

^(?:foo|bar)+$ 

working demo

if want capture invalid words, can use regex capturing groups this:

^(?:foo|bar)+|(.+)$ 

working demo

match information

match 1 1.  [9-11]  `ba` 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -