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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *