You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since node v6 (I believe), testing if an object is a regexp causes lastIndex to be reset to 0. I haven't had a chance to track it down, but my guess is executing call on it?
regexExec.call(value);
test('lastIndex', function (t) {
var re1 = /a/;
re1.lastIndex = 3;
t.ok(re1.lastIndex === 3, 'lastIndex is 3 before isRegex');
t.ok(isRegex(re1), 'is regex');
t.ok(re1.lastIndex === 3, 'lastIndex is 3 after isRegex');
t.end();
});
Running the following in node v6 and above confirms the problem for me.
var regexExec = RegExp.prototype.exec;
var re1 = /a/;
re1.lastIndex = 3;
console.log(re1.lastIndex);
regexExec.call(re1);
console.log(re1.lastIndex);
I tried with RegExp.prototype.test and from v6 onward I get the same result.
The following change appears to fix the problem with the tests that I have performed.
var tryRegexExecCall = function tryRegexExec(value) {
try {
var lastIndex = value.lastIndex;
regexExec.call(value);
value.lastIndex = lastIndex;
return true;
} catch (e) {
return false;
}
};
Although I've opened this issue here, I'm not sure that this wouldn't be more appropriate logged against an es-shim?
The text was updated successfully, but these errors were encountered:
It's certainly appropriate here; checking the type of it shouldn't cause it to be mutated.
Whether it's an es shim bug depends entirely on whether v8 made a change, and whether it was part of the spec to do so.
However, regardless, one should never rely on lastIndex of a regex you didn't create - my expectation after verifying that you had a regex would be that you'd do var copy = new RegExp(re.source, re.flags) so as to ensure not mutating re.
Since node v6 (I believe), testing if an object is a regexp causes
lastIndex
to be reset to0
. I haven't had a chance to track it down, but my guess is executingcall
on it?regexExec.call(value);
Running the following in node v6 and above confirms the problem for me.
I tried with
RegExp.prototype.test
and from v6 onward I get the same result.The following change appears to fix the problem with the tests that I have performed.
Although I've opened this issue here, I'm not sure that this wouldn't be more appropriate logged against an es-shim?
The text was updated successfully, but these errors were encountered: