Home > Mobile >  Regex: match a single word in a set inside negative lookbehind
Regex: match a single word in a set inside negative lookbehind

Time:06-27

I want to build a regex that will catch inside a text all strings (start and finish with "/') that are not inside square brackets and are not inside the patterns name()/city()/number()

This is what I got for now:

/(?<![name|city|number]\(|\[)(["'])[^\)|^\]] ?(?<!\\)\1/g

it works OK but not the best. For example: it doens't catch the strings inside a() and n(), although I want the regex to catch it.

enter image description here

link to the regex

The problem is here:

[name|city|number]

It use it as 'or' of single characters and not as 'or' of words like I want it to be.

How can I fix my regex to behave like I want without ruin the rest of it?

CodePudding user response:

Making each of those into it's own negative lookbehind seems to do the trick

(?<!name\(|\[)(?<!city\(|\[)(?<!number\(|\[)(["'])[^\)|^\]] ?(?<!\\)\1

https://regex101.com/r/E91W5a/1

  • Related