Fix es.regexp.constructor - handleNCG - consider captured groups inside non-capturing groups - #1352
Conversation
| groupid++; | ||
| } | ||
| result += chr; | ||
| groupid++; |
There was a problem hiding this comment.
It will break cases like
RegExp('foo:(?<foo>\\w+),bar:(\\w+),buz:(?<buz>\\w+)').exec('foo:abc,bar:def,buz:ghi').groups.buzsince here is a non-named group.
Could you fix it and add a couple of cases like that to the tests?
There was a problem hiding this comment.
Yes, you are right. Changes like these really break some cases.
I added an additional check to a non-capturing group after '(' character and revert the previous changes.
Also extended regexp constructor test with your example.
|
Could you fix linting issues? Note that |
oops, didn't pay attention to the linter |
|
Thanks. |
handleNCGmethod 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.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