Categories
JavaScript Answers

How to invert a regular expression in JavaScript?

Spread the love

Sometimes, we want to invert a regular expression in JavaScript.

In this article, we’ll look at how to invert a regular expression in JavaScript.

How to invert a regular expression in JavaScript?

To invert a regular expression in JavaScript, we use negative lookahead.

For instance, we write

const notFoobar = /^(?!.*foobar)/.test("foobar@example.com");

to check if "foobar@example.com" does not have 'foobar'

We use ?! right after the opening parenthesis to start negative lookahead to look for the negation of the pattern after it.

How to invert a regular expression in JavaScript?

To invert a regular expression in JavaScript, we use negative lookahead.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.