...

View Full Version : RegExp testing false in a loop



enumerator
06-02-2005, 09:24 AM
Why is this happening?


var pattern = /^\s+|\s+$/g;
var string = " spaces";

while(pattern.test(string))
{ // -> true
alert(pattern.test(string)); // -> false
break;
}

alert(pattern.test(string)); // -> true

enumerator
06-02-2005, 10:04 AM
another oddity:


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...

Harry Armadillo
06-02-2005, 11:45 AM
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...

enumerator
06-02-2005, 12:16 PM
Oh, that's odd (pardon the pun)...

Harry Armadillo
06-02-2005, 08:58 PM
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.


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

enumerator
06-02-2005, 10:17 PM
Huh, I'd bet you're right...

BubikolRamios
11-08-2010, 05:28 PM
Well, I bet there is to much code, but it works:
Aim beein, replacing all occurances of:

<div style="text-align:center;"><img src="......." /></div>
with

......



//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(),"" + mymatch1 + "");
//reset index
new_comment.match(patt);
}


Can this be done shorter ? Until I read this post and included 'reset' nothing worked.

Logic Ali
11-08-2010, 06:33 PM
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.
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum