Thursday, 15 August 2013

How does (?!) operator in regex work?

How does (?!) operator in regex work?

I thought I understand how regex operators work, but now I'm really
confused. In simplified example, I have two strings:
mail.wow.no-1.com
mail.ololo.wow.com
I want to match first one, NOT the second. And I'm writing regex
(simplified version) like this:
^mail\.(.*)(?!\.wow\.com)$
And when I run in JS method test on both those examples, it simply returns
true (in sublime 2 regex search highlights both strings, that means both
strings matched)
I know that I can make reverse regex, that will match second and make
logic depending on this, but I just want to understand how (?!) in regex
works and what am I doing wrong.
Thanks.

No comments:

Post a Comment