-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Operations to avoid .lastIndex
hacks?
#5
Comments
This library's implementation also liberally uses manual Do you envision these methods being top-level exports? I'm very open to discussion, but right now The XRegExp library (which |
Your implied proposal for new JS
|
Good points!
Below is my preliminary implementation. // Updated: 2024-07-11
function execAt(regExp, str, index) {
setLastIndex(regExp, index);
return regExp.exec(str);
}
function testAt(regExp, str, index) {
setLastIndex(regExp, index);
return regExp.test(str);
}
function setLastIndex(regExp, lastIndex) {
if (!regExp.sticky && !regExp.global) {
throw new TypeError(
`Either flag /g or flag /y must be set for matching at (or after) a particular index: ${regExp}`
);
}
regExp.lastIndex = lastIndex;
} |
|
I defer to your expertise:
I’d like to pretend that RegExp doesn’t store any state. I’d expect an actual built-in method to completely bypass There are two predecents:
My approach feels the cleanest to me but maybe we should follow the precedent of
I see two options:
In case # 2, we can ignore (I have updated the code, but not yet switched to # 2.) |
IMO it's probably overly complicated. One way to remove the complexity would be to go further than removing this and just not provide any kind of
IMO it's better to maintain the state (if not frozen) and all the expectations from precedent, for the sake of all the legacy methods that rely on
I'd be curious to hear what TC39 thinks, but I think these might not be the right precedents.
I agree with you on wishing that
Aside: My high-level view is that ES3's flag |
Another aside: There are a few different approaches that could be used to simplify, clean up, and improve JS's regex APIs. And I'll encourage and be happy to help anyone who wants to build their own version, based on whatever they think is best. Especially if it's someone like the awesome @rauschma who is well informed by all the existing complexity and legacy! 😊 Unfortunately, I don't think many people will use it if it's not native (added through new TC39 proposals), since JS's native APIs are good enough, and most people's usage of regexes is very simple. The idea that there's not much appetite for non-native solutions to this stems from my experience with XRegExp. XRegExp already provided the APIs to totally disregard |
Good point.
Makes sense.
I would argue:
I very much agree! I used I’m wondering if the upcoming flag |
This is a good explanation for your PoV--thanks! I think I still favor the flag-agnostic approach I described, but this helps me understand where you're coming from, and it's a totally reasonable perspective.
You're very generous to credit me for inspiring this. This is a very ambitious proposal that essentially deprecates a whole swath of things (including recently added features like A couple things that might be relevant/helpful:
|
Gonna go ahead and close since this proposal now has a home and it seems like the proposed methods might be more appropriate as their own polyfill rather than as new exports of |
Are there any ideas for operations so that we don’t need
.lastIndex
hacks anymore (I use them to tokenize efficiently)?Maybe (with better names...):
matchAt(string, regex, pos): {match, newPos}
String.prototype.matchAt()
testAt(regex, string, pos): {isMatch, newPos}
RegExp.prototype.testAt()
The text was updated successfully, but these errors were encountered: