Closed Bug 189898 Opened 22 years ago Closed 21 years ago

Broken String.replace: "XaXY".replace("XY", "--") gives --aXY

Categories

(Rhino Graveyard :: Core, defect)

x86
Linux
defect
Not set
normal

Tracking

(Not tracked)

VERIFIED FIXED

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
CC to Roger, this is a regression since 1.5R3
Shorter title
Summary: Broken String.replace: "XaXY".replace("XY", "--") gives --aXY instaed of Xa-- → Broken String.replace: "XaXY".replace("XY", "--") gives --aXY
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
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!
Testcase added to JS testsuite: mozilla/js/tests/ecma_3/String/regress-189898.js Currently passing in SpiderMonkey, failing in Rhino -
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.
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
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
Yes, now www.microsoft.com works with the Rhino CVS version in our browser.
Status: VERIFIED → CLOSED
Status CLOSED is deprecated as per bug 169885
Status: CLOSED → REOPENED
Resolution: FIXED → ---
Re-Resolving
Status: REOPENED → RESOLVED
Closed: 22 years ago21 years ago
Resolution: --- → FIXED
verifying former CLOSED bugs
Status: RESOLVED → VERIFIED
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
Fixing wrong milestone assignment
Target Milestone: 1.5R5 → 1.5R4
You need to log in before you can comment on or make changes to this bug.