Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mozilla Regular Expression Bug? 2

Status
Not open for further replies.

tsdragon

Programmer
Dec 18, 2000
5,133
US
I've been tearing my hair out over this one, and it appears to be a bug in Mozilla's regular expression engine. If I run the same re test over the same data in IE I get the same result every time, just as you would expect. BUT, if I do the same thing in FireFox I get alternating True and False results when the data matches the regular expression (it works fine if the data doesn't match the re). Here's the code I set up to demonstrate the problem:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html>
<head>
<title>Test RE</title>
<script type="text/javascript">
function testit() {
	if ( /^\d{3}$/g.test(document.forms['daform'].elements['danum'].value) ) {
		document.getElementById('daresult').innerHTML = "TRUE";
	} else {
		document.getElementById('daresult').innerHTML = "FALSE";
	}
}
</script>
</head>
<body>
<form name="daform">
<input type="text" name="danum" id="danum" value="123" />&nbsp;
<input type="button" name="dabutton" value="TEST" onclick="testit();">&nbsp;
(Three Digits)
</form>
<span id="daresult" style="font-weight:bold;"> </span>
</body>
</html>
If you don't want to type it in, use this link.

Try it in both IE and FF. Click the test button repeatedly. In IE it will yield correct results. In FF it will alternate between True and False when the value is valid (but stay false when the value is invalid).

Any explanation?

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Tracy,

this is not a bug - it is actually proper behavior when you use the global flag with test()

i had the same experience a while back... see here


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
It does look like a genuine moz bug - I get the same results. I also tried seperating the elements out into local variables, with no change to the results:
Code:
function testit() {
	[COLOR=red]var data = document.forms['daform'].elements['danum'].value;
	var regex = /^\d{3}$/g;[/color]

    if ( regex.test(data) ) {
        document.getElementById('daresult').innerHTML = "TRUE";
    } else {
        document.getElementById('daresult').innerHTML = "FALSE";
    }
}
interestingly, if you remove the global identifier g it appears to work.

It also works if you use the String's match method:
Code:
	var data = document.forms['daform'].elements['danum'].value;
	var regex = /^\d{3}$/g;

    if ( [COLOR=red]data.match(regex)[/color] ) {

And if you use the constructor method:
Code:
	var data = document.forms['daform'].elements['danum'].value;
	var regex = [COLOR=red]new RegExp("^\\d{3}$", "g");[/color]

    if ( regex.test(data) ) {


But honestly, 'daform'?! 'danum'?! Have you no shame? ;-)

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
ah cross-posting...useful link Jeff - thanks!

So perhaps not a Moz bug after all - but unexpected behaviour to say the least.

It seems the regex remains persistant even if you use the keyword [tt]var[/tt] or explicitly set [tt]regex=null;[/tt] otherwise the [tt]lastIndex[/tt] property would be reset when the function exits and the variable [tt]regex[/tt] loses scope.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Thanks to jemminger and manarth for the great posts. You both get a star!

So, I understand why it works the way it does in Mozilla, and why you might want to take advantage of that behavior (under some strange circumstances). I guess in this case it is IE that actually has the bug (like that never happens).

I'll just remove the global flag and it should work like I want.

(Tek-Tips comes thru once again!)

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top