I have an array of phrases, and am trying to detect if a string of text contains a full phrase. I currently am using the following regex:
var arrOfWords = ['foo', 'bar', 'foo bar']
var regEx = new RegExp('\\b(' + arrOfWords.join('|') + ')\\b', 'gi')
console.log(regEx)
/\b(foo|bar|foo bar)\b/gi
I used \b
because I didn't want to include substrings, but rather the complete word/phrase, i.e.
"foo" should not match with "foobar", but should match "I like foo"
This works great, however, word boundaries, \b
, ignore phrases that begin with #
, as \b
starts the boundary at alphanumeric characters.
So if "#hashtag" is in the array, it will only match if the string being tested has "hashtag", not "#hashtag"
What I'm really looking for would be a regex that matches the entire phrase as specified in the array, including symbols and hashes. Or maybe a solution that can work around this.
Can anyone point me in the right direction? Thanks.