Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I seeing inconsistent JavaScript logic behavior looping with an alert() vs. without it? [duplicate]

Tags:

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?

like image 910
Eric Wendelin Avatar asked Oct 16 '08 18:10

Eric Wendelin


People also ask

How to master the art of looping in JavaScript?

Master the art of looping in JavaScript with these incredible tricks 1 The “For” Loop The For Loop ... 2 The “For In” loop The For I ... 3 The “While” loop The While ... 4 The “Do While” loop In Do W ... 5 The “.forEach ()” method

What are the most common mistakes while implementing a loop?

One of the most common mistakes while implementing any sort of looping is that that it may not ever exit, that is the loop runs for infinite time. This happens when the condition fails for some reason. Writing code in comment?

What happens when condition is evaluated to true in a loop?

If it evaluated to true, then the loop body statements are executed otherwise first statement following the loop is executed. For this reason it is also called Entry control loop Once the condition is evaluated to true, the statements in the loop body are executed.

What is loops in JavaScript?

Loops in JavaScript. Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. For example, suppose we want to print “Hello World” 10 times. This can be done in two ways as shown below:


1 Answers

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 its lastIndex property:

var testRegex = /blah/ig; // logs: true 4 console.log(testRegex.test("blah blah"), testRegex.lastIndex); // logs: true 9  console.log(testRegex.test("blah blah"), testRegex.lastIndex); // logs: false 0 console.log(testRegex.test("blah blah"), testRegex.lastIndex); 

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 g flag). On the first run, it matches the first "blah", and leaves lastIndex set to 4 (the index of the space after the first "blah"). The second run starts matching at the lastIndex, matches the second blah, and leaves lastIndex set to 9 - one past the end of the array. The third run doesn't match - lastIndex is bogus - and leaves lastIndex set 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 lastIndex set to the length of the string just tested. Meaning, if you were to call test() twice, the second test would always fail:

var filterRegex = /.*blah.*/ig; // logs: true, 9 console.log(filterRegex.test("blah blah"), filterRegex.lastIndex); // logs: false, 0  console.log(filterRegex.test("blah blah"), filterRegex.lastIndex); 

Fortunately, since you create your regex immediately prior to calling test(), and never call test() more than once, you'll never run into unexpected behavior... Unless you're using a debugger that lets you add in another call to test() on the side. Yup. With Firebug running, a watch expression containing your call to test() will result in intermittent false results 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:

var filterRegex = /.*blah.*/i; // logs: true, 0 console.log(filterRegex.test("blah blah"), filterRegex.lastIndex); // logs: true, 0  console.log(filterRegex.test("blah blah"), filterRegex.lastIndex); 

Suggestions

  • Avoid the global flag when you don't need it.
  • Be careful what you evaluate in the debugger: if there are side effects, it can affect the behavior of your program.
like image 110
Shog9 Avatar answered Sep 28 '22 08:09

Shog9