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

Named capture groups collision with RegExp exec result #2

Closed pgilad closed 7 years ago

pgilad commented 8 years ago

There could be name collisions with exec result properties, namely:

Should collisions be validated? Ignored? Rejected?

barneycarroll commented 8 years ago

How about hanging the named results off a new exec property hash, eg values, whereby:

let {one, two} = /^(?<one>.*):(?<two>.*)$/.exec('foo:bar').values;
pgilad commented 8 years ago

That would solve it

littledan commented 8 years ago

See discussion in the explainer of this. An earlier version of this proposal used a 'groups' object. I think that's too unergonomic, so I suggested just banning those properties. The prototype of match objects is null so there is no issue with Array methods.

littledan commented 7 years ago

@barneycarroll There is a big advantage to what you're suggesting, which is that it's future-proof for adding more properties to the match object (since it would be likely web-incompatible to create new early errors for using group names). I was previously thinking that this is unlikely to happen, but people keep raising this concern to me. Maybe it wouldn't be so unergonomic to put the named groups in a groups property. Any thoughts? cc @bterlson

littledan commented 7 years ago

Not an issue because it's on the groups property.