How do I reverse the pattern match?

advertisements

Here is my string:

Organization 2
[email protected] [email protected] [email protected] [email protected]
Organization 3
[email protected] [email protected]

Also this is my pattern:

/^.*[email protected][^ ]+|^.*$/gm

As you see in the demo, the pattern matches this:

Organization 2
[email protected]
Organization 3
[email protected]


My question: How can I make it inverse? I mean I want to match this:

[email protected] [email protected] [email protected]
[email protected]

How can I do that? Actually I can write a new (and completely different) pattern to grab expected result, but I want to know, Is "inverting the result of a pattern" possible?


No, I don't believe there is a way to directly inverse a Regular Expression but keeping it the same otherwise.

However, you could achieve something close to what you're after by using your existing RegExp to replace its matches with an empty string:

var everythingThatDidntMatchStr = str.replace(/^.*[email protected][^ ]+|^.*$/gm, '');