I’ve been debugging this one application for a while and it led me to this test case. When I run it in firefox 3.6.x it only works 50% of the time.
var success = 0;
var pat = /(\d{2})\/(\d{2})\/(\d{4})\s(\d{2}):(\d{2})\s(am|pm)/g;
var date = "08/01/2011 12:00 am";
for(var i=0;i<100;i++) if(pat.exec(date)) success++;
alert("success: " + success + " failed: " + (100 - success));
it alerts success: 50 failed: 50
what’s going on here?
The
gflag means that, after the first match, the second search starts at the end of the matched substring (i.e., at the end of the string), and fails, resetting the start position to the beginning of the string.from MDC docs for RexExp.exec(). (See also RegExp.lastIndex)