> var p = /abc/gi;
> var s = "abc";
> p.test(s);
true
> p.test(s);
false;
When I run this code on console of Chrome I Have this output above. Each time I call '.test()' I get a diferent value. Someone could explain to me why this happens? thanks
The behavior is due to the "g" modifier, i.e. matches three times, no match the fourth time:
> var p = /a/gi;
> var s = "aaa";
> p.test(s)
true
> p.test(s)
true
> p.test(s)
true
> p.test(s)
false
See similar question: Why RegExp with global flag in Javascript give wrong results?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With