Index: test/mjsunit/string-replace.js |
diff --git a/test/mjsunit/string-replace.js b/test/mjsunit/string-replace.js |
index 6b022df307d8e76e8f4a4e40cbaf22efc5901e1a..502a7a4bd4a270369b933d7a6dcf7a71bc553586 100644 |
--- a/test/mjsunit/string-replace.js |
+++ b/test/mjsunit/string-replace.js |
@@ -212,3 +212,56 @@ var str = 'She sells seashells by the seashore.'; |
var re = /sh/g; |
assertEquals('She sells sea$schells by the sea$schore.', |
str.replace(re,"$$" + 'sch')) |
+ |
+ |
+var replace_obj = { length: 0, toString: function() { return "x"; }}; |
+assertEquals("axc", "abc".replace(/b/, replace_obj)); |
+ |
+var search_obj = { length: 1, toString: function() { return "b"; }}; |
+assertEquals("axc", "abc".replace(search_obj, function() { return "x"; })); |
+ |
+var regexp99pattern = ""; |
+var subject = ""; |
+for (var i = 0; i < 99; i++) { |
+ regexp99pattern += "(.)"; |
+ subject += String.fromCharCode(i + 24); |
+} |
+ |
+function testIndices99(re) { |
+ // Test $1 .. $99 |
+ for (var i = 1; i < 100; i++) { |
+ assertEquals(String.fromCharCode(i + 23), |
+ subject.replace(re, "$" + i)); |
+ } |
+ |
+ // Test $01 .. $09 |
+ for (var i = 1; i < 10; i++) { |
+ assertEquals(String.fromCharCode(i + 23), |
+ subject.replace(re, "$0" + i)); |
+ } |
+ |
+ assertEquals("$0", subject.replace(re, "$0")); |
+ assertEquals("$00", subject.replace(re, "$00")); |
+ assertEquals(String.fromCharCode(10 + 23) + "0", |
+ subject.replace(re, "$100")); |
+} |
+ |
+testIndices99(new RegExp(regexp99pattern)); |
+testIndices99(new RegExp(regexp99pattern, "g")); |
+ |
+var regexp59pattern = ""; |
+for (var i = 0; i < 59; i++) regexp59pattern += "(.)"; |
+ |
+function testIndices59(re) { |
+ // Test $60 .. $99. Captures reach up to 59. Per spec, how to deal |
+ // with this is implementation-dependent. We interpret $60 as $6 |
+ // followed by "0", $61 as $6, followed by "1" and so on. |
+ var tail = subject.substr(59); |
+ for (var i = 60; i < 100; i++) { |
+ assertEquals(String.fromCharCode(i / 10 + 23) + (i % 10) + tail, |
+ subject.replace(re, "$" + i)); |
+ } |
+} |
+ |
+testIndices59(new RegExp(regexp59pattern)); |
+testIndices59(new RegExp(regexp59pattern, "g")); |