javascript - Trouble with word-boundary (\b) -


i have array of keywords, , want know whether @ least 1 of keywords found within string has been submitted. further want absolutely sure keyword has been matched, , not similar word.

say, example, our keywords [english, eng, en] because looking variation of english.

now, input user i h8 eng class, or equally provocative , illiterate - eng should matched. should fail match word england or odd thing chen, though it's got en bit.

so, in infinite lack of wisdom believed along lines of in order match 1 of array items input:

.match(regexp('\b('+array.join('|')+')\b','i')) 

with thinking regular expression matches array, presented (english|eng|en) , see whether there zero-width word bounds on either side.

you need double backslashes.

when create regex regexp() constructor, you're passing in string. javascript string constant syntax also treats backslash meta-character, quoting quotes etc. thus, backslashes stripped out before regexp() code runs!

by doubling them, step of parsing string leave 1 backslash behind. regexp() parser see single backslash before "b" , right thing.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -