4

If the user enters exactly "null", I want the regex match to fail. Entering "xxxnullxxx" is ok however.

The following regex rejects "null" but it also rejects any string containing "null", which I don't want.

^(?!.*null).*$
Jin Kim
  • 16,562
  • 18
  • 60
  • 86

2 Answers2

4

Add $ and remove .* from the look ahead:

^(?!null$).*

The trailing $ isn't needed.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

This will match everything except for null: ^(?!(?:null)$).*$ I got the idea from here.

Community
  • 1
  • 1
Martin Cup
  • 2,399
  • 1
  • 21
  • 32