-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
regex sticky? #63
Comments
the var text = 'First line\nsecond line';
var regex = /(\S+) line\n?/y;
var match = regex.exec(text);
console.log(match[1]); // prints 'First'
console.log(regex.lastIndex); // prints '11'
var match2 = regex.exec(text);
console.log(match2[1]); // prints 'Second'
console.log(regex.lastIndex); // prints '22'
var match3 = regex.exec(text);
console.log(match3 === null); // prints 'true' From the same page, more clarification (bolded the part that should clear up an important use case for this flag): The "y" flag indicates that it matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes). This allows the match-only-at-start capabilities of the character "^" to effectively be used at any location in a string by changing the value of the lastIndex property. |
@getify FWIW, last year I documented how You’re right that there is a lot of misinformation about this feature. Firefox/SpiderMonkey shipping a broken implementation and not fixing it for a long time (and the description on MDN matching that brokenness) didn’t really help. Also check the V8 tests for this feature. |
Turns out there's a fair bit of very ES6 specific stuff to support sticky in a very particular way. Seems definitely like an ES6 feature to call out.
FWIW, I found that example (and others like it) to be really terrible at explaining what I needed to know about It shows only the happy path (where subsequent matches are already adjacent by virtue of cooperation between known string contents and the way the pattern is structured), shows no failure or reset of index cases, and also uses only |
Absolutely fair! My (incorrect) assumption was that the |
Your examples are definitely clearer. Appreciate that. They don't explicitly illustrate how a match failure resets [Edit]: Also, I would mention that the fact that your example strings ( |
Actually, I think that's the old FF behavior, it appears ES6's |
Finally found spec text that perfectly clarifies
So, |
Wow, really good to know. |
FWIW, I've completely rewritten (and expanded) my book section on "sticky mode" after all these explorations. If it helps at all, here how I went about it: https://github.com/getify/You-Dont-Know-JS/blob/master/es6%20&%20beyond/ch2.md#sticky-flag If there's interest, I'll happily author a significantly reduced summary version of that discussion for this README, and submit a PR. Just let me know. |
I'm currently trying to figure out what the heck the
y
regex sticky mode actually does (lots of varied examples that conflict, and mis-information), but I was surprised to see it's not listed here at all. Plans to cover it?If I get to some sort of complete understanding of it soon, I'll submit a PR. But perhaps you might already have a good way to explain it that would clear up the confusion?
The text was updated successfully, but these errors were encountered: