Regex match inverse group in javascript

I want to match strings that don't have abc, defor ghi. Opposite:

/(abc|def|ghi)/

How do I cancel this? I do not want

/(^abc|^def|^ghi)/

because there will be more “logic." (If that’s even what he does.)

How do I cancel an entire group match (or whatever it is called)?

(I'm trying to beat 5. at http://regex.alf.nu/ )

+4
source share
4 answers

Use a negative view:

/^(?!.*?(abc|def|ghi)).*$/
+9
source

You need to define a capture group, including start ( ^) and end of line ( $), or you will get false positive matches:

/^((?!(abc|def|ghi)).)*$/

:

:

  • ABC
  • bobabc
  • abcjoe

: http://regex101.com/r/yI3tF4

+3
+3
source

Use of negative references:

/^(?!.*abc)(?!.*def)(?!.*ghi)/
/^(?!.*(abc|def|ghi))/
0
source

All Articles