slevithan / xregexp

Extended JavaScript regular expressions
http://xregexp.com/
MIT License
3.31k stars 278 forks source link

Error "Cannot use same name for multiple groups" in IE, works in Chrome #290

Closed ynagorny closed 4 years ago

ynagorny commented 4 years ago

new ("^foo (?<a>(?:'[^']*'|\"[^\"]*\"|[^'][^\\s]*[^']|[^\"][^\\s]*[^\"]|[^'\"])) bar (?<b>.+) lorem");

throws Cannot use same name for multiple groups (?<b> in IE11, but works in Chrome.

This regexp does not have a duplicate group b, thus this seems like a bug.

More details:

The smallest reproducible case seems to be:

new XRegExp("^(?<a>(?:.))(?<b>.+)");

Looks like the culprit is the nested unnamed group, when removed, the regexp compiles:

new XRegExp("^(?<a>.)(?<b>.+)");

ynagorny commented 4 years ago

Preliminary research shows that this is due to IE not supporting the 'y' flag. When parsing a regexp it tries to apply /\(\?P?<([\w$]+)>/gy to the regexp in Chrome, but /\(\?P?<([\w$]+)>/g in IE. When parsing the nested anonymous group (?:.) in IE it skips ahead and matches the b group prematurely.

Looks like the fix should be to make sure that the match occurs at the last index rather than somewhere esle.

ynagorny commented 4 years ago

After further review, looks like this is due to core-js (3.4.0) interfering with xregexp. Still don't know how to resolve.

ynagorny commented 4 years ago

Turns out babel was not setting up proper polyfills for ie. However, fixing the issue with polyfills resulted in xregexp going into infinite loop. Decided to remove xregexp as it was not used extensively.