regex negative lookahead to match everything but a

2019-08-11 11:44发布

问题:

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:

You must use anchor $ also:

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

RegEx Demo