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 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question RegExp testing false in a loop

    Why is this happening?

    Code:
    var pattern = /^\s+|\s+$/g;
    var string = "   spaces";
    
    		while(pattern.test(string))
    		{ // -> true
    			alert(pattern.test(string)); // -> false
    			break;
    		}
    
    alert(pattern.test(string)); // -> true
    Thanks in advance!

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    another oddity:

    Code:
    var pattern = /^\s+|\s+$/g;
    var string = "   spaces";
    
    while(pattern.test(string))
    	alert(/^\s+|\s+$/g.test(string)); // -> true, and the loop breaks... why?
    only literal regular expressions can be used in a loop, I take it...
    Thanks in advance!

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var pattern = /^\s+|\s+$/g;
    var string = "   spaces";
    
    alert(pattern.test(string)); // -> true
    alert(pattern.test(string)); // -> false
    alert(pattern.test(string)); // -> true
    alert(pattern.test(string)); // -> false
    
    var pattern = /^\s+|\s+$/;
    alert(pattern.test(string)); // -> true
    alert(pattern.test(string)); // -> true
    Your use of the global modifier g seems to be confusing test. I don't know why...

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, that's odd (pardon the pun)...
    Thanks in advance!

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What I've pieced together:

    When you use the .test() or .exec() methods with a RegExp-object that has the global modifier set, the RegExp-object stores a hidden index of it's position in the string. The idea being that you loop through to globally apply whatever you're doing to the string. The index persists until a match fails, even if you switch strings.

    String methods that take RegExp's as a parameter do global in a single call -- they don't use the index, but do reset it.
    Code:
    reg=/a/g;
    s="aab";
    
    alert(reg.test(s));// true
    alert(reg.test(s));// true
    alert(reg.test(s));// false, resets index
    alert(reg.test(s));// true
    alert(s.match(reg)); // "a,a" also resets index
    alert(reg.test(s));// true
    alert(reg.test(s));// true
    alert(reg.test(s));// false

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Huh, I'd bet you're right...
    Thanks in advance!

  • #7
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Well, I bet there is to much code, but it works:
    Aim beein, replacing all occurances of:
    Code:
    <div style="text-align:center;"><img src="......." /></div>
    with
    Code:
    [IMG]......[/IMG]
    Code:
    //new_comment .. contains string ....
    patt = new RegExp(/<div style="text-align: center;"><img src=".*?<\/div>/ig);
    while (patt.test(new_comment) == true)
        {
          //reset index
          new_comment.match(patt);
          mymatch = patt.exec(new_comment);
          patt1 = new RegExp(/http:\/\/.*?"/ig);
          mymatch1 = patt1.exec(mymatch);
          mymatch1 = mymatch1.toString().substring(0,mymatch1.toString().length-1);
          new_comment = new_comment.replace(mymatch.toString(),"[IMG]" + mymatch1 + "[/IMG]");
          //reset index
          new_comment.match(patt);
        }
    Can this be done shorter ? Until I read this post and included 'reset' nothing worked.
    Last edited by BubikolRamios; 11-08-2010 at 05:31 PM.
    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

  • #8
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    When you use the g flag, it's lastIndex property is moved past the point of the last match so that it knows where to start looking for the next.
    Code:
    var pattern = /^\s+|\s+$/g;
    var string = "   spaces";
    
    		while(pattern.test(string))
    		{ 
                        pattern.lastIndex = 0;
                        alert(pattern.test(string)); // -> true 
                        pattern.lastIndex = 0;
                        break;
    		}
    
    alert(pattern.test(string)); // -> true


  •  

    Posting Permissions

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