I have one task: there is a collection of keywords by default, and when a user enters a new keyword, he must filter by the existing collection of keywords and inform the user about the duplicate.

A duplicate keyword is considered to be at the beginning of a line or separated by commas, i.e.

// start line keyword1 ,

// start line keyword2,

// within the string is some text, keyword1, some text, keyword2,

Not a duplicate: text keyword1, keyword2 text, text keyword1 text, keyword1 keyword2, keyword1x, xkeyword1.

This is a regular expression.

/(?<!\w|\w\s)(keyword1|keyword2)(?!\w|\s\w)/gmi 

works great in Chrome, but does not work in Safari or Firefox, because the "lookbehind"

 (? <! ...) 

is not yet supported in these browsers.

How can I process lookbehind with another reg expression?

  • Replace (?<!\w|\w\s) with (\w\s*)? and after the match is found, check the value of the first trap. If it is not empty, the match is false, otherwise valid. - Wiktor Stribiżew

0