I have code similar to this filtering entries in an Array of Objects:
var filterRegex = new RegExp('.*blah.*','ig'); if (filterRegex.test(events[i].thing) && events[i].show) { console.log('SUCCESS: filtering thing ' + i + ' ' + events[i].thing); events[i].show = false; numevents--; }
I get inconsistent results with this if condition (checking with Firebug, both conditions are true individually, but sometimes the whole expression evaluates to false). HOWEVER, if I actually put an alert() called inside this if statement (like line 4), it becomes consistent and I get the result I want.
Can you see anything wrong with this logic and tell me why it’s not always producing what is expected?
Ok, i see it now. The key to your problem is the use of the
g(global match) flag: when this is specified for a regex, it will be set up such that it can be executed multiple times, beginning each time at the place where it left off last time. It keeps a ‘bookmark’ of sorts in itslastIndexproperty:The above example creates an instance of a very simple regex: it matches ‘blah’, upper or lower case, anywhere in the string, and it can be matched multiple times (the
gflag). On the first run, it matches the first ‘blah’, and leaveslastIndexset to 4 (the index of the space after the first ‘blah’). The second run starts matching at thelastIndex, matches the second blah, and leaveslastIndexset to 9 – one past the end of the array. The third run doesn’t match –lastIndexis bogus – and leaveslastIndexset to 0. A fourth run would therefore have the same results as the first.Now, your expression is quite a bit more greedy than mine: it will match any number of any characters before or after ‘blah’. Therefore, no matter what string you test on, if it contains ‘blah’ it will always match the entire string and leave
lastIndexset to the length of the string just tested. Meaning, if you were to calltest()twice, the second test would always fail:Fortunately, since you create your regex immediately prior to calling
test(), and never calltest()more than once, you’ll never run into unexpected behavior… Unless you’re using a debugger that lets you add in another call totest()on the side. Yup. With Firebug running, a watch expression containing your call totest()will result in intermittentfalseresults showing up, either in your code or in the watch results, depending on which one gets to it first. Driving you slowly insane…Of course, without the g flag, livin’ is easy:
Suggestions