Closed
Bug 286259
Opened 19 years ago
Closed 18 years ago
Regexp literal reuses old RegExp object
Categories
(Core :: JavaScript Engine, defect)
Core
JavaScript Engine
Tracking
()
People
(Reporter: magnusrk+bugzilla, Unassigned)
References
()
Details
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041217 Regexp literal notation creates one global RegExp object and makes all use of the literal in repeated function calls refer to this one object. This makes .lastIndex, exec() and test() very unreliable. Reproducible: Always Steps to Reproduce: 1. Call a function including var a = /someregexp/ and store result. 2. Call the function again and compare local var with stored previous result. or 1. Call a function including var a = /someregexp/g and alert the lastIndex, which is 0. 2. Call exec() or test() on the regexp at least once. 3. Call function again and notice the lastIndex alert is not 0. (Calling enough times will return the lastIndex to zero and then restart the cycle, so in a few cases lastIndex becomes 0 after all.) Actual Results: Mozilla reports they are === (the same RegExp object). or Mozilla reports that lastIndex is not 0. Expected Results: Mozilla reports that they are not the same object. or Mozilla reports that lastIndex is 0. I am aware that this behavior is according to the ECMA specification, but from a usage point of view it's unacceptable. When using 'var re = /regexp/;' in a function, the regexp should be (or at least seem to be) created and perish with the variable. When the object lives on and returns with repeated references as currently, lastIndex, test() and exec() are no longer trustworthy. As the testcase shows, other literal object notations do not create global objects and act as references to it like regexp currently does. Suggested fix: Make lastIndex reset to 0 every time a literal is accessed, or eventually create a new RegExp object each time. Workarounds: Don't use regexp literal notation in functions that might be called more than once, or manually reset .lastIndex to 0 every time you reference a regexp literal.
Comment 1•19 years ago
|
||
Stop filing bugs in bugzilla.mozilla.org about what you regard as flaws in the ECMA-262 (ISO-16262) spec. Take it to ECMA, and try to address the compatibility issue: how will all the deployments and implementations possibly handle current ECMA and a future version that does what you want. /be
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
Updated•18 years ago
|
Status: VERIFIED → UNCONFIRMED
Resolution: INVALID → ---
Comment 3•18 years ago
|
||
*** This bug has been marked as a duplicate of 98409 ***
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago → 18 years ago
Resolution: --- → DUPLICATE
You need to log in
before you can comment on or make changes to this bug.
Description
•