Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match reverse group in javascript

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/)

like image 698
Rudie Avatar asked Dec 06 '22 04:12

Rudie


2 Answers

Use negative lookaead:

/^(?!.*?(abc|def|ghi)).*$/
like image 197
anubhava Avatar answered Dec 19 '22 19:12

anubhava


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:

  • bob
  • joe

This will not match:

  • abc
  • def
  • ghi
  • bobabc
  • abcjoe

See it in action here: http://regex101.com/r/yI3tF4

like image 32
brandonscript Avatar answered Dec 19 '22 19:12

brandonscript