RESOLVED FIXED 9234
Implement $&, $' and $` replacement codes in String.prototype.replace
https://bugs.webkit.org/show_bug.cgi?id=9234
Summary Implement $&, $' and $` replacement codes in String.prototype.replace
mitz
Reported 2006-06-02 08:42:14 PDT
According to Ecma-262 15.5.4.11, if replaceValue is a string, then $& should be replaced with the matched substring, $` with everything preceding the matched substring, and $' with everything following the matched substring. For example, "abcdefg".replace(/d(e)/, "[$&]") == "abc[de]fg" (currently achievable using $0) "abcdefg".replace(/d(e)/, "[$`]") == "abc[abc]fg" "abcdefg".replace(/d(e)/, "[$']") == "abc[fg]fg"
Attachments
Patch without test case and change log (2.31 KB, patch)
2006-06-02 08:45 PDT, mitz
no flags
Patch including change log and test (7.28 KB, patch)
2006-06-02 14:48 PDT, mitz
ggaren: review+
mitz
Comment 1 2006-06-02 08:45:45 PDT
Created attachment 8665 [details] Patch without test case and change log This passes all JavaScriptCore tests and WebCore layout test, but I didn't know what kind of test to attach to it. Does it need to be a JSC test?
Geoffrey Garen
Comment 2 2006-06-02 11:28:53 PDT
Comment on attachment 8665 [details] Patch without test case and change log for (int i = 0; (i = substitutedReplacement.find(UString("$"), i)) != -1; i++) { This line confused the dickens out of me, because 'i = 0' has no meaning, and 'i++' has meaning only as a side-effect in the test that follows it. I know it's not something you changed, but consider one of these (maybe my perception is skewed): int i = -1; while ((i = substitutedReplacement.find(UString("$"), i + 1)) != -1) OR for (int i = substitutedReplacement.find(UString("$")); i != -1; i = substitutedReplacement.find(UString("$"), i + 1)) This comment would change, too: i += backrefLength - 1; // -1 offsets i++ (--> 'i + 1') Anyway, looks good. We discussed how to write a layout test, so I'm going to clear the review bit for now.
mitz
Comment 3 2006-06-02 14:48:54 PDT
Created attachment 8671 [details] Patch including change log and test This patch also adds double-digit back references ($nn) as required by the spec.
David Kilzer (:ddkilzer)
Comment 4 2006-06-02 21:52:38 PDT
Committed revision 14705.
mitz
Comment 5 2006-12-19 10:10:43 PST
*** Bug 7919 has been marked as a duplicate of this bug. ***
Note You need to log in before you can comment on or make changes to this bug.