-
-
Notifications
You must be signed in to change notification settings - Fork 893
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 lastIndex #403
Comments
Indeed, I see that in IE 6, 7, and 8, I'll look into this; thanks for the report! |
@espretto where do you see this being worked around with |
my mistake, it wasn't |
Thanks, I'll see what I can do. This seems like it's only a bug when |
unfortunately, i've seen this bug occur on non-empty strings, too. i think the question is whether or not the regex matches zero or more of something. for example this yields different results, too: var re = /[a-z]*/g
re.lastIndex = 1
re.exec('s')
re.lastIndex === 2 // in IE6, expected empty string match at pos 1 instead
re.lastIndex = 1
re.exec('s1')
re.lastIndex === 2 // in IE6, expected empty string match at pos 1 instead for a patch i'm thinking (not tested): var hasLastIndexBug = (function () {
var re = /.*/g
re.exec('')
return re.lastIndex !== 0
}())
if (hasLastIndexBug) {
var nativeExec = RegExp.prototype.exec
RegExp.prototype.exec = function exec (string) {
var match = nativeExec.call(this, string)
if (match && !match[0].length) this.lastIndex--
/* or alternatively
if (match && !match[0].length) this.lastIndex = match.index
* or most explicit
if (match && !match[0].length) this.lastIndex = match.index + match[0].length
*/
return match
}
} it seems the state-machine forgets to reset its look-ahead pointer before it bails out. |
the regex
lastIndex
bug seems to persist although es5shim is included.I've seen this is being worked around to patch String#replace but RegExp#exec is none the wiser.
The text was updated successfully, but these errors were encountered: