< /p>
> var re = /.*?bl.*gr.*/gi;
undefined
> re
/.* ?\bbl.*\bgr.*/gi
> re.test("Blue-Green");
true
> re.test("Blue-Green");
false
> re.test("Blue-Green");
true
> re.test("Blue-Green");
false
However, test the same regular expression as text:
> /.*?bl.*gr.*/gi.test("Blue- Green");
true
> /.*?bl.*gr.*/gi.test("Blue-Green");
true
> /. *?bl.*gr.*/gi.test("Blue-Green");
true
> /.*?bl.*gr.*/gi.test(" Blue-Green");
true
I cannot explain this, it makes debugging very difficult. Can anyone explain this behavior?
See this question.
When you write text, you get a new regular expression object every time, so you lose the lastIndex state associated with the old object.
I have this behavior in Chrome (Developer Tools) and Firefox (Firebug). Please note that the regular expression test returns alternating true/false values:
< p>
> var re = /.*?bl.*gr.*/gi;
undefined
> re
/.*?\bbl .*\bgr.*/gi
> re.test("Blue-Green");
true
> re.test("Blue-Green");
false
> re.test("Blue-Green");
true
> re.test("Blue-Green");
false
However, test the same regular expression as text:
> /.*?bl.*gr.*/gi.test("Blue-Green");
true
> /.*?bl.*gr.*/gi.test("Blue-Green");
true
> /.*?bl .*gr.*/gi.test("Blue-Green");
true
> /.*?bl.*gr.*/gi.test("Blue-Green" );
true
I cannot explain this, it makes debugging very difficult. Can anyone explain this behavior?
/ g (global) regexps will do this, yes.
See this question.
< p>When you write text, you get a new regular expression object every time, so you lose the lastIndex state associated with the old object.