Closed Bug 334795 Opened 18 years ago Closed 18 years ago

RegEx exec() maintains state even though regex gets reinitialized

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 98409

People

(Reporter: christian, Unassigned)

Details

Attachments

(1 file)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2

Evaluating regular expressions with exec() and using the "g" modifier maintains state although the regular expression get initialized again. Even worse, the number of matches from the first exec() call is maintained for subsequent exec() calls.

Reproducible: Always
Steps to Reproduce:
1. Load Attachment (it creates the regex /(a*(b*))c+/g and than calls exec() with parameter "abc c"; then, the returned array is shown on the screen)
2. Click on the button. 
3. Click on the button again.
4. Note the results

Actual Results:  
After the first click: 0: abc  1: ab  2: b
After the second click: 0: c  1:  2:  

Expected Results:  
0: abc  1: ab  2: b after both clicks
Note: even when the second click should return the second match (which in my opinion is not the desired behavior, since the regular expression gets instantiated again), the length of the array returned by exec() should be 1 and not 3. The value 3 is taken from the first result (changing the regex proves that).
see also bug 237111

*** This bug has been marked as a duplicate of 98409 ***
Status: NEW → RESOLVED
Closed: 18 years ago
Resolution: --- → DUPLICATE
(In reply to comment #2)
> see also bug 237111
> *** This bug has been marked as a duplicate of 98409 ***

aah, didn't find this. Thanks! So the fact that the literal regex maintains state is a dup and will be fixed once ECMA-262 edition 4 is implemented. But what about the array of matches keeping the same size? I think this is not covered in the bugs you mentioned. What do you think?
The length of the array depends only on the number of capturing parentheses,
no matter if they matched or not (here they even matched the empty string):

js> var re1= /(a*(b*))c+/g
js> re1.exec( "abc c").toSource()
["abc", "ab", "b"]
js> re1.exec( "abc c").toSource()
["c", "", ""]
js> var re2= /(a(b)*)*c+/g
js> re2.exec( "abc c").toSource()
["abc", "ab", "b"]
js> re2.exec( "abc c").toSource()
["c", undefined, undefined]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: