Node Boy
Node Boy

Reputation: 31

regex negative lookahead to match everything but a specific string

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?

Upvotes: 3

Views: 667

Answers (1)

anubhava
anubhava

Reputation: 786091

You must use anchor $ also:

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

RegEx Demo

Upvotes: 2

Related Questions