Friday, June 22, 2018

A regex to match a substring that isn't followed by a certain other substring > Find Any Word Not Followed by a Specific Word > Regex Negative Lookbehind

The problem is I need to match a word say "match" in a string. It is very easy using the expression /(match)/gi
Above expression is very easy. Now original problem is need to match word "match" not starting with any other word like 10 or 20 or any word. To do so we need to use negative lookbehind. Below is a complete regex that will match word match but not start with 10 or 20 or '.

/(?<!(10|20))(?<!')(match)/gi
Meaning of the above regex is will match a word match but not followed by 10 or 20 or '

Output of this regex is as below

Explanation of this regex:


No comments:

Post a Comment