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.