tc39 / proposal-regexp-named-groups

Named capture groups for JavaScript RegExps
https://tc39.github.io/proposal-regexp-named-groups/
222 stars 21 forks source link

Clarify if numbered capturing groups will also be exposed via the 'groups' property #22

Closed SebastianZ closed 7 years ago

SebastianZ commented 7 years ago

It is not clear (at least not in the description) whether numbered capturing groups will also be exposed through the groups property and whether numbered and named groups can be mixed resp. how the result will look like in this case.

Sebastian

schuay commented 7 years ago

Numbered captures will not be exposed through groups. It's also not possible to specify numbers as capture names (e.g. /(?<1>.)/) since group names need to match IdentifierName.

Every named capture is also a numbered capture.

var result = /(.)(?<a>.)\2\k<a>/.exec("abbb");  // ["abbb", "a", "b"]
result.groups;  // {a: "b"}
SebastianZ commented 7 years ago

Though this makes the handling of captured groups inconsistent. For consistency, I think it would be better to turn the groups property into an array-like object and add the numbered groups as references to the result[x] values. (This would effectively make the result[x] items except result[0] redundant and allow to deprecate them in the future.)

Sebastian

littledan commented 7 years ago

I don't see the inconsistency. The groups object is for the named groups, and the match object is for the numbered groups. It seems odd to put the numbered groups in two places. I don't see a way that we can go back and deprecate the match object, or why we would want to do so.

SebastianZ commented 7 years ago

The groups object is for the named groups, and the match object is for the numbered groups.

That's the inconsistency. The two types of captured groups are put at two different places.

It seems odd to put the numbered groups in two places.

Yes, that's the downside of this approach, but required for the reasons outlined above.

I don't see a way that we can go back and deprecate the match object, or why we would want to do so.

The reason is to sanitize the match object's structure. The idea is to logically separate the main match from the capture groups.

Sebastian

schuay commented 7 years ago

Creating groups also has performance implications - that was the reason to create groups only when the RegExp has named captures.