Closed Bug 571875 Opened 14 years ago Closed 14 years ago

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

Categories

(Firefox :: General, defect)

3.6 Branch
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 98409

People

(Reporter: hacklor, Unassigned)

Details

Attachments

(1 file)

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.
Removing the global repeat does return true each time.
Version: unspecified → 3.6 Branch
According to a friend the bug does not occur in the 3.7a6pre nightly build.
persumably a duplicate of bug 98409.
Status: UNCONFIRMED → RESOLVED
Closed: 14 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: