2

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

Clown Man
  • 385
  • 1
  • 3
  • 12

1 Answers1

1

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.

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • You can't use identically named capturing groups in JavaScript RegExp, that pattern of yours throws a "*`Duplicate capture group name`*" exception. `XRegExp` also throws "*`Cannot use same name for multiple groups`*". – Wiktor Stribiżew Dec 30 '20 at 17:00