RegExp.lastIndex not properly initialized when assigned to variable

RESOLVED DUPLICATE of bug 98409

Status

()

Core
JavaScript Engine
--
minor
RESOLVED DUPLICATE of bug 98409
14 years ago
14 years ago

People

(Reporter: Jeff Grimes, Unassigned)

Tracking

Trunk
x86
Windows XP
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Reporter)

Description

14 years ago
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040616
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040616

See below.

Reproducible: Always
Steps to Reproduce:
<html>
<head>
<script>

function evalRegExp () {
    var re1 = /blah/g;
    var text = "blahblahblahblahblah";
    var result = re1.exec(text);
    document.writeln("<p>re1.lastIndex=" + re1.lastIndex);
}    

evalRegExp();
evalRegExp();

</script>
</head>
<body>
</BODY>
</HTML>

Actual Results:  
Document contains the following output:

re1.lastIndex=4

re1.lastIndex=8 

Expected Results:  
Document should contain the following output:

re1.lastIndex=4

re1.lastIndex=4
(Reporter)

Updated

14 years ago
Summary: RegExp.lastIndex not properly initiated when assigned to variable → RegExp.lastIndex not properly initialized when assigned to variable
(Reporter)

Comment 1

14 years ago
Changing Severity to minor since this bug can be worked around by using "new 
RegExp()" instead of regex literals.

function evalRegExp () {
    var re1 = new RegExp("blah", "g");
    var text = "blahblahblahblahblah";
    var result = re1.exec(text);
    document.writeln("<p>re1.lastIndex=" + re1.lastIndex);
}  
Severity: major → minor
DUP of INVALID bug 98409.

/be

*** This bug has been marked as a duplicate of 98409 ***
Status: UNCONFIRMED → RESOLVED
Last Resolved: 14 years ago
Resolution: --- → DUPLICATE
(Reporter)

Comment 3

14 years ago
Created attachment 154031 [details]
Test page

This may be the same as bug 98409, but if so, I don't believe that 98409 was
properly resolved as invalid.

Note from comment #1 that the behavior is different if the regular expression
is initialized using "new RegExp" rather than a regular expression literal. 

Contrast this with the behavior for assigning arrays to variables.  The
attached test page demonstrates this further.  It seems to be that
evalRegExpLiteral() should behave identically to evalRegExpNew().
(Reporter)

Updated

14 years ago
Status: RESOLVED → UNCONFIRMED
Resolution: DUPLICATE → ---
Bug 98409 was resolved properly.  A literal regexp makes an instance of class
"RegExp" that contains properties such as lastIndex, which persist from method
invocation to later invocations.  A new RegExp created each time in a loop is a
different object, each time.

Please don't reopen bugs without reading the ECMA spec and citing chapter and
verse to support your position.

/be

*** This bug has been marked as a duplicate of 98409 ***
Status: UNCONFIRMED → RESOLVED
Last Resolved: 14 years ago14 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.