Regular expression with global repeat in test() function gives unexpected result

RESOLVED DUPLICATE of bug 98409

Status

()

RESOLVED DUPLICATE of bug 98409
9 years ago
8 years ago

People

(Reporter: hacklor, Unassigned)

Tracking

3.6 Branch
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Reporter)

Description

9 years ago
User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)

Testing the following regular expression in Javascript using the test() function gives unexpected results each time you test it.

/abc/g.test("abcdeabc")

Sometimes it returns true other times false.

Reproducible: Always

Steps to Reproduce:
The following HTML code:

<html>
	<head>
		<script type="text/javascript" charset="utf-8">
			var num_passed = 0;
			var num_failed = 0;
			var times_tested = 0;
			var value = "abcdeabc";
		
			function reg_exp_test( value ) {
				var num_passed = 0;
				var num_failed = 0;
				var times_tested = 0;
				
				for(var i=0; i<10000; i++) {
					times_tested++;
					if( /abc/g.test(value) ) {
						num_passed++;
					} else {
						num_failed++;
					}
				}
				
				document.write("Value: '"+value+"'<br />");
				document.write("Times tested: "+times_tested+"<br />");
				document.write("Num passed: "+num_passed+"<br />");
				document.write("Num failed: "+num_failed+"<br />");
				document.write("<br />");
			}
		</script>
	</head>
	<body>
		<h1>Regular Expression test</h1>
		<script type="text/javascript" charset="utf-8">
			reg_exp_test( "abcde" );
			reg_exp_test( "abcdeabc" );
			reg_exp_test( "abcdeabcdeabc" );
		</script>
	</body>
</html>
Actual Results:  
Value: 'abcde'
Times tested: 10000
Num passed: 5000
Num failed: 5000

Value: 'abcdeabc'
Times tested: 10000
Num passed: 6667
Num failed: 3333

Value: 'abcdeabcdeabc'
Times tested: 10000
Num passed: 7500
Num failed: 2500

Expected Results:  
You would expect that if you test the same regular expression with the same value you would get a true or a false form the test function consistently. (Like in IE or Safari, Opera shows the same problem)



It seems that the cursor for testing a regular expression with a global repeat does not reset to the beginning of the value for each test.
(Reporter)

Comment 1

9 years ago
Removing the global repeat does return true each time.
Version: unspecified → 3.6 Branch
(Reporter)

Comment 2

9 years ago
According to a friend the bug does not occur in the 3.7a6pre nightly build.
persumably a duplicate of bug 98409.

Comment 4

8 years ago
Created attachment 484996 [details]
Test file showing this bug. The regular expressions work every other time in the loop. Works correctly in Chrome.

Updated

8 years ago
Status: UNCONFIRMED → RESOLVED
Last Resolved: 8 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 98409
You need to log in before you can comment on or make changes to this bug.