- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move named backreferences to a groups object #175
Comments
I like that, as this would make the type definitions for things like Typescript much better. I currently have to use an "any-cast", which is very bad practice in Typescript: let pattern = XRegExp('a(?<groupName>.)c')
let match = XRegExp.exec('abc', pattern)
console.log((match as any).groupName) (because "groupName" is not a defined property in the "RegExpExecArray" type, used by the type definitions for XRegExp over at definitelyTyped) and extending it with dynamic properties is nearly impossible or inducing any-types, again) |
What's the reasoning for allowing |
Coming to think about it: wouldn't it be better to redo the whole stuff and not return an augmented array, but an object with the keys:
As for proto: yes, the methods should perhaps disallow certain keywords and throw. |
The intention was to use this as an opportunity to simplify the rules of XRegExp's regex syntax. Fewer syntax rules makes it easier to describe, learn, and emulate in other tools. That there would be a "valid" pattern that you shouldn't use (at least in JS-land) because of undesired results has some precedent. E.g. patterns that can lead to catastrophic backtracking are not blocked at the syntax level by any regex engine. That said, I don't feel strongly about this, and I'd be fine with it if
That seems reasonable. An alternative name for The downside of this (in addition to the breaking change itself) would be divergence from the standard JS RegExp match object spec, making XRegExp a little harder to learn and adjust to. |
I actually found it a bit harder to learn, because of the augmented objects. I actually expected an "XRegExp"-object because of the broader functionality. 😁 |
I'm still not too informed/opinionated on this issue generally, but it looks like https://github.com/tc39/proposal-regexp-named-groups is stage 3 now, so I'm definitely in favor of matching that as closely as possible, as described in the first comment in this thread:
|
Yes, definitely! That is the proper way to go. |
Great, I've started working on an implementation, and will likely have a WIP PR open later today.
As it turns out, I was mistaken about this causing undesired results, since the |
Implemented by @josephfrazier in #210, now landed. |
This should probably be documented somewhere, I had to dig quite a bit to find this issue and understand what was going on in the PR. I'm glad this was implemented tho! |
FWIW, there's a little discussion about the website update process in #205 (comment) |
The |
Right now, looping over named backreferences after executing a regex with named groups is unnecessarily difficult, since XRegExp augments match arrays with the named backreferences as direct properties. It would be nice to give access to a clean object with named groups.
Proposal:
XRegExp.install
/uninstall
/isInstalled
.groups
object on result arrays.String.prototype.replace
replacement functions (where named backreferences are added as properties of the first String argument), maybe leave things as they are (nogroups
object). If https://github.com/tc39/proposal-regexp-named-groups moves beyond the proposal stage, then XRegExp should follow wherever ECMAScript is headed.length
and__proto__
should no longer be reserved words for named captures. Using__proto__
as a capture name would still lead to undesired results, so don’t do that.As a workaround for now, here's how you can get a clean object with named groups:
Moved here from #45 (comment) since that was a long thread.
The text was updated successfully, but these errors were encountered: