Closed
Bug 189898
Opened 22 years ago
Closed 21 years ago
Broken String.replace: "XaXY".replace("XY", "--") gives --aXY
Categories
(Rhino Graveyard :: Core, defect)
Tracking
(Not tracked)
VERIFIED
FIXED
1.5R4
People
(Reporter: user, Assigned: rogerl)
Details
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20021003
Build Identifier: The third release candidadte of Rhino 1.5R4
The following scripts demonstartes the problem:
var result = "XaXY".replace("XY", "--");
var expect = "Xa--"
var ok = result == expect;
if (ok) {
print("OK");
} else {
print("FAILED: expected='"+expect+"', result='"+result+"'");
}
It looks like replace in general always match the first character if it starts
the match string
Reproducible: Always
Steps to Reproduce:
Run the above script in the rhino shell
Actual Results:
FAILED: expected='Xa--', result='--aXY'
Expected Results:
OK
Reporter | ||
Comment 1•22 years ago
|
||
CC to Roger, this is a regression since 1.5R3
Reporter | ||
Comment 2•22 years ago
|
||
Shorter title
Summary: Broken String.replace: "XaXY".replace("XY", "--") gives --aXY instaed of Xa-- → Broken String.replace: "XaXY".replace("XY", "--") gives --aXY
Comment 3•22 years ago
|
||
Confirming and reassigning to Roger.
CURRENT SPIDERMONKEY (as is; without patch for bug 85721)
js> "XaXY".replace("XY", "--")
Xa--
CURRENT RHINO
js> "XaXY".replace("XY", "--")
--aXY
and yet:
js> "XaXY".search("XY")
2
Assignee: nboyd → rogerl
Comment 4•22 years ago
|
||
FWIW:
Note there is an important difference between the replace() method
and the search() and match() methods of String.prototype, when the
first parameter provided is a string and not a regexp.
This was a last-minute change made in the ECMA-262 Edition 3 spec.
As late as the Final Draft for Edition 3, the spec used to require
these to be equivalent:
str.replace(strA, strB) == str.replace(new RegExp(strA),strB)
However, in bug 83293 Jim Ley pointed out that ECMA-262 Edition 3 Final
changed on this. String.prototype.replace (searchValue, replaceValue),
if provided a searchValue that is not a RegExp, is NOT to replace it with
new RegExp(searchValue)
but rather:
String(searchValue)
This puts the replace() method at variance with search() and match(),
which continue to follow the RegExp conversion of the Final Draft.
See ECMA-262 Edition 3 Final, 15.5.4.11 String.prototype.replace
This is of relevance if strings contain any regexp meta-characters
like '$', '^', etc. If such a string is provided as a first parameter
to the replace() method, these characters are to be interpreted as
string literals, unlike the case for search() and match().
Finally, note the place to get the ECMA-262 Edition 3 Final is at
http://www.mozilla.org/js/language (also note the list of errata).
I am told that the version at the ECMA website itself is not current!
Comment 5•22 years ago
|
||
Testcase added to JS testsuite:
mozilla/js/tests/ecma_3/String/regress-189898.js
Currently passing in SpiderMonkey, failing in Rhino -
Reporter | ||
Comment 6•22 years ago
|
||
Note for String.prototype.replace semantics in MSIE:
www.microsoft.com contains JavaScript with expressions like:
<html-fragment1>.replace(<html-fragment2>, <html-fragment3>)
where <html-fragment2> contains at least . and ? and often JavaScript fragments
from even handlers with +, [] etc.. So it is essential there that
<html-fragment2> is treated as a string, not RegExp.
Assignee | ||
Comment 7•22 years ago
|
||
The REOP_FLAT node being generated for the 'flat' entry-point used by replace
was constructed wrong. I also added error checks for bad quantifiers from
SpiderMonkey bug 188206
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → FIXED
Comment 8•22 years ago
|
||
Verified FIXED.
The testcase for this bug is now passing in both the interpreted
and compiled modes of Rhino:
mozilla/js/tests/ecma_3/String/regress-189898.js
Furthermore, as Roger indicated, Rhino is also now passing the
testcase for bug 188206:
mozilla/js/tests/ecma_3/RegExp/regress-188206.js
This test deliberately misuses regexp quantifiers and checks
for SyntaxErrors to result. This is now working in Rhino -
Status: RESOLVED → VERIFIED
Reporter | ||
Comment 9•22 years ago
|
||
Yes, now www.microsoft.com works with the Rhino CVS version in our browser.
Status: VERIFIED → CLOSED
Comment 10•21 years ago
|
||
Status CLOSED is deprecated as per bug 169885
Status: CLOSED → REOPENED
Resolution: FIXED → ---
Comment 11•21 years ago
|
||
Re-Resolving
Status: REOPENED → RESOLVED
Closed: 22 years ago → 21 years ago
Resolution: --- → FIXED
You need to log in
before you can comment on or make changes to this bug.
Description
•