Non capturing branch reset regex in NodeJS

Viewed 228

https://regex101.com/r/UXnhTy/1

var date = /(?|(Sat)ur|(Sun))day/;

console.log(date.exec("Sunday"));

This fails with:

SyntaxError: Invalid regular expression: /(?|(Sat)ur|(Sun))day/: Invalid group

Is there a version of NodeJS that supports this? Or some library out there that

I tested this with nodejs v8.12.0

1 Answers

Not really. An advanced alternative regex library for JavaScript is XRegExp, but it doesn't have the feature you're after - not even as an addon.
A simpler regex feature that is supported by XRegExp is named capture groups, so you can write:

var days = XRegExp('(?:(?<d>Sat)ur|(?<d>Sun))day', 'gi');

You can't use numbers as group names, but named groups should fit what your needs - they allow backreferences (using \k<d>), replacement (${d}), capturing (match.d), and all features of a regular numbered group.

Named captured groups is supported natively by ES2018: ES2018 Regular Expression Updates.
According to node.green, named capture groups are supported by Node.js ≥10.3.0, or by ≥8.6.0 with the --harmony flag.

Related