Regular expression tests cannot make a decision between True and False (JavaScript)

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.

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.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5473 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.