Reputation: 53
I have a string and two words from a dictionary and I am trying to know if in my string there are words that are not my dictionary words.
var string = 'foobarfooba';
var patt = new RegExp("[^(foo|bar)]");// words from dictionary
var res = patt.test(string);
console.log(res);//return false
It should return true because in my string there is also 'ba', but it return false.
Upvotes: 0
Views: 1384
Reputation: 30995
Same as Phil commented in your question, you have to get rid of the character class:
[^(foo|bar)]
^-- here --^
Character classes are used to match (or not match if you use ^
) specific unsorted characters.
Just use:
var patt = new RegExp("(?:foo|bar)");// words from dictionary
If you want to ensure that all the string matches your regex, you can use:
^(?:foo|bar)+$
If you want to capture the invalid words, you can use a regex with capturing groups like this:
^(?:foo|bar)+|(.+)$
Match information
MATCH 1
1. [9-11] `ba`
Upvotes: 1