A regular expression that looks right can still be wrong in ways that only show up on real data: a pattern that hangs on long input, one that matches far more than intended, or one that passes every test and then fails the second time it is called. These are the mistakes we find most often in code review, with what goes wrong and the fix. Every example was checked in Node.js, and they behave the same in browsers.
1. Catastrophic backtracking
/^(a+)+$/.test("aaaaaaaaaaaaaaaaaaaaaaaaaX");
This pattern looks harmless, but when the match fails the engine tries every possible way of dividing the as between the inner and outer + before giving up. The number of ways doubles with each extra character. In our test, 20 characters took about 50 milliseconds and 26 characters took half a second; a few more would take minutes. On a server, one request like this can block a Node.js process completely, which is why it is called a regular expression denial of service (ReDoS).
The warning signs are a quantifier inside another quantifier, such as (a+)+ or (\w+\s?)*, and alternatives inside a repeated group that can match the same text, such as (a|a)* or (\d+|\d+\.\d+)*.
Fix: remove the nesting. ^a+$ matches the same strings and fails instantly. Limit the length of input before you match it. If users can supply the pattern itself, use an engine that does not backtrack, such as RE2.
2. Greedy .* matching too much
const html = "<div>first</div><div>second</div>";
html.match(/<div>.*<\/div>/)[0];
// "<div>first</div><div>second</div>"
.* is greedy: it runs to the end of the text and then backs up only as far as it must, so the match runs from the first <div> to the last </div>.
Fix: make it lazy with .*?, or say exactly what is allowed with a negated class:
html.match(/<div>.*?<\/div>/g); // ["<div>first</div>", "<div>second</div>"]
html.match(/<div>[^<]*<\/div>/g); // same result, and faster
For real HTML, use a parser such as DOMParser in the browser. Nested and malformed tags defeat any regex sooner or later.
3. Not escaping special characters
/Only $5.00!/.test("Only $5.00!"); // false
$ means "end of input", so this pattern can never match: nothing comes after the end. And . means "any character", so once you escape the $, 5.00 still matches 5x00.
Fix: escape them, as in /Only \$5\.00!/. When the text comes from a variable, escape it in code. RegExp.escape() does this in Chrome and Edge 136, Firefox 134 and Safari 18.2 or later. For older browsers and Node.js, use this function:
const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
new RegExp(escapeRegExp("$5.00 (approx)")); // /\$5\.00 \(approx\)/
4. Anchors that do not cover the whole pattern
/^cat|dog$/.test("catapult"); // true
/^cat|dog$/.test("hotdog"); // true
The | has the lowest priority in a regex, so this means "cat at the start" or "dog at the end", not "exactly cat or dog".
Fix: group the alternatives inside the anchors:
/^(?:cat|dog)$/.test("catapult"); // false
Without any anchors at all, /admin/ matches "superadmin" and "administrator", which matters a great deal in validation code.
5. Reusing a regex with the g flag
const isA = /a/g;
isA.test("a"); // true
isA.test("a"); // false
isA.test("a"); // true
A regex with the g (or y) flag remembers where its last match ended in lastIndex, and test() and exec() start from there next time. Store one in a constant and use it to validate several values, and every second call can fail.
Fix: leave out g when you only need a yes or no answer, create the regex inside the function, or reset isA.lastIndex = 0 before each use. String.prototype.match, matchAll and replace do not have this problem.
6. Assuming \w covers all letters
/^\w+$/.test("café"); // false
In JavaScript, \w means only [A-Za-z0-9_], so names such as Zoë, José or Łukasz fail validation.
Fix: use Unicode property escapes with the u flag:
/^[\p{L}\p{N}_]+$/u.test("café"); // true
\p{L} is any letter in any script and \p{N} any digit. (\s, on the other hand, already matches non-breaking spaces and other Unicode whitespace in JavaScript.)
7. Writing [A-z] for letters
/^[A-z]+$/.test("a_b^c[d]"); // true
Character ranges follow character codes, and between Z (90) and a (97) sit [, \, ], ^, _ and the backtick. [A-z] lets all six through.
Fix: write [A-Za-z], or use the i flag with [a-z].
8. Validating emails and URLs too strictly
Long patterns for email addresses reject real addresses, such as ones with a + or a long top-level domain, and still let through addresses that do not exist. A loose check is more useful:
const looksLikeEmail = (s) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s);
Then confirm the address by sending an email to it. For URLs, skip the regex and use the parser built into JavaScript, which follows the standard exactly:
const isHttpUrl = (s) => {
try {
const u = new URL(s);
return u.protocol === "https:" || u.protocol === "http:";
} catch {
return false;
}
};
Test against the cases that break things
A regex is only as good as the inputs you tried. Before shipping one, test it against an empty string, very long input, input with accents or emoji, input with the special characters it is meant to reject, and the same input twice in a row. You can paste the pattern and your test text into our Regex Tester to see every match and capture group, using the same JavaScript engine your code will run on. For a deeper look at lookahead and lookbehind, see our guide to lookarounds.
Frequently Asked Questions
How do I stop a regex freezing my server?
Avoid nested quantifiers such as (a+)+ and overlapping alternatives inside a repeated group, limit the length of input before matching, and for patterns supplied by users use an engine without backtracking, such as RE2 (the re2 package in Node.js).
What is the difference between greedy and lazy matching?
A greedy quantifier such as .* matches as much as it can and then gives characters back if the rest of the pattern fails. A lazy one such as .*? matches as little as it can and takes more only when needed.
Why does my regex work in one language but not another?
Engines differ in details such as lookbehind support, named group syntax, what \w and \s match and which flags exist. Test in the language that will run the pattern.