Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

How about this one...

    js> r = RegExp("asdf", "g"); 
    /asdf/g 
    js> r.test("asdf") 
    true 
    js> r.test("asdf") 
    false 
    js> r.test("asdf") 
    true 
    js> r.test("asdf") 
    false
Surprisingly this is not a bug, though I was certain it was at first. When using the global flag the RegExp object will keep track of the last matching character position and start the next comparison at that position (after the 4th character, in this case) which results in this seemingly incorrect alternation between "asdf" matching "asdf". Removing the global flag produces correct results.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: