I want to match strings that don't have abc
, def
or ghi
. The opposite is easy:
/(abc|def|ghi)/
How do I reverse that? I don't want to
/(^abc|^def|^ghi)/
because there's going to be more 'logic' in there. (If that's even what it does.)
How do I reverse the whole group match (or whatever it's called)?
(I'm trying to beat 5. on http://regex.alf.nu/)
Use negative lookaead:
/^(?!.*?(abc|def|ghi)).*$/
You need to define the capture group including the start (^
) and end of the string ($
), or you'll end up with false positive matches:
/^((?!(abc|def|ghi)).)*$/
This will match:
This will not match:
See it in action here: http://regex101.com/r/yI3tF4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With