Fix es.regexp.constructor - handleNCG - consider captured groups inside non-capturing groups by Ulop · Pull Request #1352 · zloirock/core-js (original) (raw)
handleNCG method from es.regexp.constructor does not work correctly in some cases.
If named captured group placed inside non-capturing group(or other group, that doesn't follow (?<group_name>...) pattern) then group id counter extra incremented.
case chr === '(': if (exec(IS_NCG, stringSlice(string, index + 1))) { index += 2; ncg = true; // where increment is needed } result += chr; groupid++; // where actually incremented continue;
Real example: H_REGEX from vscode repository.const H_REGEX = /(?<tag>[\w\-]+)?(?:#(?<id>[\w\-]+))?(?<class>(?:\.(?:[\w\-]+))*)(?:@(?<name>(?:[\w\_])+))?/;
When it used through RegExp constructor new RegExp('(?<tag>[\\w\\-]+)?(?:#(?<id>[\\w\\-]+))?(?<class>(?:\\.(?:[\\w\\-]+))*)(?:@(?<name>(?:[\\w\\_])+))?'), exec method return wrong groups values. Tested on 'div.editor.original@original' input value.
With polyfill:
{ "tag": "div", "id": ".editor.original", "name": undefined, "class": "original" }
Native:
{ "tag": "div", "id": undefined, "class": ".editor.original", "name": "original" }
Link to test