I’ve encountered an issue using the regex function exec() in Firefox 10 and 11.
The function seems to behave erratic when it is called a lot. Among the correct result, it also returns null a lot. From Safari 5.1.3, Chrome 18 and the above mentioned Firefox versions, I can see the issue only within Firefox.
I’ve created a JSFiddle to demonstrate the problem: http://jsfiddle.net/KSH3S/ , source:
var i, x = "";
for (i = 0; i < 10000; i++) {
var matches = /foo/g.exec('sdkfjfooasdknal');
x += matches + "<br>";
}
$('body').html(x);
In my two Firefox versions, this returns 40x foo, 1x null, 41x foo, and from then on it swaps between these two on every single call.
Have you encountered this?
The problem has something to do with the implicit declaration of the regex inside the loop. My guess is that the browser is caching it somewhere or getting confused by that somehow.
If you explicitly create a new regex object each time through the
forloop, then Firefox no longer has a problem with this:http://jsfiddle.net/jfriend00/F49db/
And, it doesn’t matter which way you explicitly declare the regex as this method works also: