Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts

    regex test problem

    How do you explain that ?

    Code:
    regexPattern = new RegExp("a","gi");
    s = "a";
            alert (regexPattern.test(s));//--> true
            alert (regexPattern.test(s));//--> false
            alert (regexPattern.test(s));//--> true
            alert (regexPattern.test(s));//--> false
            //......
    EDIT: if I modify each alert:
    Code:
     alert (regexPattern.test(s) + s.match(regexPattern));
    obviously result of
    Code:
     s.match(regexPattern)
    is the same on each alert.
    Last edited by BubikolRamios; 12-09-2012 at 09:26 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Hmm. Very odd! You seem to have exposed a bug (not a flower! ) in the Javascript regex engine. Same in IE and Chrome.

    Code:
    var s = "a";
    alert (/a/gi.test(s)); // true
    alert (/a/gi.test(s)); // true
    alert (/a/gi.test(s)); // true
    alert (/a/gi.test(s)); // true

    "If you make it idiot proof, they'll build a better idiot"

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Found a flower or bug
    Bugs also there (-:

    Yeah, tested the thing in chrome, before submiting here.
    Not to mention hours lost, before 'lamp' turned on: "Damn, there something smell bad about .test".
    I had .test in a loop and "a" string has been changing and on every second loop bad result.
    Last edited by BubikolRamios; 12-09-2012 at 11:21 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match.
    Mozilla
    Code:
    regexPattern = new RegExp("a","gi");
    s = "a";
            alert (regexPattern.test(s));//--> true
            alert(regexPattern.lastIndex); // 1
            alert (regexPattern.test(s));//--> false
            alert(regexPattern.lastIndex); // 0
            alert (regexPattern.test(s));//--> true
            alert(regexPattern.lastIndex); // 1
            alert (regexPattern.test(s));//--> false
            alert(regexPattern.lastIndex); // 0
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If you remove the "g" global modifier - which is not relevant to test() - then it behaves as expected.

    Added: I won't have JavaScript besmirched
    (only joshing!)
    Last edited by AndrewGSW; 12-09-2012 at 11:27 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by AndrewGSW View Post
    If you remove the "g" global modifier - which is not relevant to test() - then it behaves as expected.
    You are right! Woot!
    We learn something every day. Sometimes something worth knowing!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    It is the same thing with prompt or confirm and if an "a" take place of s !

    In short (See this MDN page *) you have to restore the regexPattern.lastIndex to 0 before each new test !

    Good to know. Thanks !

    (*) Or better ECMAScript Language Specification 15.10.6.2 and 15.10.6.3


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •