0

I have some task: exist default keywords collection, and when user input new keyword, it must be filtering by exist keyword collection and inform user about duplicate.

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

//start line keyword1,

//start line keyword2,

// inside line some text, keyword1, some text, keyword2,

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

This regular expression

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

works fine in chrome, but does not work in safari or firefox, because "lookbehind"

(? <! ...)

,is not yet supported in these browsers.

How can i handle lookbehind another reg expression?

Sorry, for my English.

Evgeniy R
  • 142
  • 1
  • 9
  • you can translate litterally your requirements instead of using lookbehind/lookaheads: "the beginning of the line or separated by commas": `(?:^|,\s?)(keyword1|keyword2)(?:$|\s?,)`, [see in Regex101](https://regex101.com/r/73fKQ5/1) (added support of space before the coma or end of line for the end of the regex, but you can use `(?:,)` if you just need the coma) – Kaddath Apr 08 '19 at 10:32
  • https://regex101.com/r/73fKQ5/2 if add one more duplicate keyword it`s not working – Evgeniy R Apr 08 '19 at 11:43
  • You're right, the end part must stay a lookahead, or else the coma is consumed and cannot be detected for next match. It wasn't working for consecutive keywords. [updated here](https://regex101.com/r/73fKQ5/3) with `(?:^|,\s?)(keyword1|keyword2)(?=$|\s?,)` – Kaddath Apr 08 '19 at 11:51
  • You're welcome, always glad to help – Kaddath Apr 08 '19 at 12:41
  • @Kaddath can you help me - how get a range of matched keywords in the string. Because "indexOf" returns the coordinates of first match. Like it implemented on the Regex101 – Evgeniy R Apr 08 '19 at 12:55
  • I suggest you take a look at the "code generator" option (verify first the language used). If you have anything that you want to know (and don't find it already on SO), you should alsk a separate question. I may look if I have time, but busy right now – Kaddath Apr 08 '19 at 13:18
  • @Kaddath yes, ok thanks – Evgeniy R Apr 08 '19 at 13:34

0 Answers0