regex negative lookahead to match everything but a specific string

664 views Asked by At

I am using this regex to match any string other than foo:

^((?!(foo)).)*

It succeeds in matching and capturing anything other than foo but it also matches foo, just with no capture. Is there a way to make it not match foo at all?

1

There are 1 answers

0
anubhava On

You must use anchor $ also:

^(?:(?!foo).)*$

RegEx Demo