3

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
  • 15,074
  • 16
  • 56
  • 82

2 Answers2

3

Add $ and remove .* from the look ahead:

^(?!null$).*

The trailing $ isn't needed.

Bohemian
  • 389,931
  • 88
  • 552
  • 692
1

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

Community
  • 1
  • 1
Martin Cup
  • 2,141
  • 1
  • 19
  • 28