SECURITY WARNING: Never run commands you don't understand. Always review code before execution. Use at your own risk.
Regex New Added 15 October 2026

JavaScript: Invalid regular expression: Duplicate capture group name

Reusing a named group in different alternatives, as in (?<year>\d{4})-\d{2}|\d{2}/(?<year>\d{4}), only became legal in ES2025. Chrome 125, Firefox 129 and Safari 17 accept it; older browsers, older Node versions and some bundler parsers throw at the moment the script loads. A pattern that passes every test on your laptop can therefore break the whole bundle for a share of real users.

Quick fix

Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.

Quick fix
// Does this runtime support it? Throws on older engines
node -e "new RegExp('(?<y>a)|(?<y>b)'); console.log('ok', process.version)"

// Portable version: give each alternative its own name
const re = /(?<isoYear>\d{4})-\d{2}|\d{2}\/(?<usYear>\d{4})/;
const m = re.exec(input);
const year = m?.groups.isoYear ?? m?.groups.usYear;

// Or split into two simpler patterns tried in order
const year2 = (/(\d{4})-\d{2}/.exec(input) ?? /\d{2}\/(\d{4})/.exec(input))?.[1];

// Check which engines you need to support
npx browserslist

How to diagnose Regex errors

Regex problems come in two flavours: it does not match what you expect (usually escaping, greediness, or an engine feature difference) and it matches but takes forever. The second is catastrophic backtracking, a genuine denial-of-service vector known as ReDoS, caused by nested quantifiers over overlapping character classes, such as (a+)+b. Any regex applied to untrusted input should be checked for it.

If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.

  1. Test against a visualiser that shows backtracking steps, and always test with input that fails to match. That is where catastrophic backtracking appears, not on successful matches.
  2. Eliminate nested quantifiers over overlapping classes. Rewrite (a+)+ as a+, and prefer possessive quantifiers or atomic groups where the engine supports them.
  3. Know your engine: lookbehind, named groups and Unicode property escapes differ between PCRE, RE2, JavaScript, Python and Go. RE2 (used by Go) deliberately has no backtracking and rejects some patterns outright.
  4. Anchor patterns with ^ and $ where you mean a whole-string match. Unanchored patterns silently match substrings.
  5. Set a timeout or use a linear-time engine for user-supplied patterns. Never run an untrusted regex on a request thread.

Tools worth reaching for

  • regex101.com
  • re2 / RE2J
  • Python re.DEBUG
  • grep -P for PCRE testing
  • recheck / redos linters

Authoritative references

Primary documentation for this error, worth reading before applying any fix in production.

developer.mozilla.org github.com

Related Regex errors

See all 17 Regex errors →

Browse other categories

Something missing or wrong?

This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.