I'm trying to match a string (using a Perl regex) only if it doesn't start with "abc:" or "defg:", but I can't seem to find out how. I've tried something like
^(?:(?!abc:)|(?!defg:))
Lookbehind, which is used to match a phrase that is preceded by a user specified text. Positive lookbehind is syntaxed like (? <=a)something which can be used along with any regex parameter. The above phrase matches any "something" word that is preceded by an "a" word.
Because the lookahead is negative, this means that the lookahead has successfully matched at the current position. At this point, the entire regex has matched, and q is returned as the match.
Negative lookahead That's a number \d+ , NOT followed by € . For that, a negative lookahead can be applied. The syntax is: X(?! Y) , it means "search X , but only if not followed by Y ".
JavaScript doesn't support any lookbehind, but it can support lookaheads.
Lookahead (?=foo)
, (?!foo)
and lookbehind (?<=foo)
, (?<!foo)
do not consume any characters.
You can do multiple assertions:
^(?!abc:)(?!defg:)
or:
^(?!defg:)(?!abc:)
...and the order does not make a difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With