2 "String.replace(…) test"
5 var testString
= "It's the end of the world as we know it, and I feel fine.";
7 "\"It's the end of the world as we know it, and I feel fine.\"");
8 shouldBe("testString.replace('end','BEGINNING')",
9 "\"It's the BEGINNING of the world as we know it, and I feel fine.\"");
10 shouldBe("testString.replace(/[aeiou]/gi,'-')",
11 "\"-t's th- -nd -f th- w-rld -s w- kn-w -t, -nd - f--l f-n-.\"");
12 shouldBe("testString.replace(/[aeiou]/gi, function Capitalize(s){ return s.toUpperCase(); })",
13 "\"It's thE End Of thE wOrld As wE knOw It, And I fEEl fInE.\"");
14 shouldBe("testString.replace(/([aeiou])([a-z])/g, function Capitalize(){ return RegExp.$1.toUpperCase()+RegExp.$2; })",
15 "\"It's the End Of the wOrld As we knOw It, And I fEel fIne.\"");
16 shouldBe("testString.replace(/([aeiou])([a-z])/g, function Capitalize(orig,re1,re2) { return re1.toUpperCase()+re2; })",
17 "\"It's the End Of the wOrld As we knOw It, And I fEel fIne.\"");
18 shouldBe("testString.replace(/(.*)/g, function replaceWithDollars(matchGroup) { return '$1'; })", "\"$1$1\"");
19 shouldBe("testString.replace(/(.)(.*)/g, function replaceWithMultipleDollars(matchGroup) { return '$1$2'; })", "\"$1$2\"");
20 shouldBe("testString.replace(/(.)(.*)/, function checkReplacementArguments() { return arguments.length; })", "\"5\"");
22 // replace with a global regexp should set lastIndex to zero; if read-only this should throw.
23 // If the regexp is not global, lastIndex is not modified.
26 function testReplace(_re
, readonly
)
31 re
= Object
.defineProperty(re
, 'lastIndex', {writable
:false});
32 return '0x1x2'.replace(re
, replacer
);
36 shouldBe("testReplace(/x/g, false)", '"0y1y2"');
37 shouldThrow("testReplace(/x/g, true)");
38 shouldBe("testReplace(/x/, false)", '"0y1x2"');
39 shouldBe("testReplace(/x/, true)", '"0y1x2"');
40 shouldBe("testReplace(/x/g, false); re.lastIndex", '0');
41 shouldThrow("testReplace(/x/g, true); re.lastIndex");
42 shouldBe("testReplace(/x/, false); re.lastIndex", '3');
43 shouldBe("testReplace(/x/, true); re.lastIndex", '3');
45 replacer = function() { return 'y'; };
46 shouldBe("testReplace(/x/g, false)", '"0y1y2"');
47 shouldThrow("testReplace(/x/g, true)");
48 shouldBe("testReplace(/x/, false)", '"0y1x2"');
49 shouldBe("testReplace(/x/, true)", '"0y1x2"');
50 shouldBe("testReplace(/x/g, false); re.lastIndex", '0');
51 shouldThrow("testReplace(/x/g, true); re.lastIndex");
52 shouldBe("testReplace(/x/, false); re.lastIndex", '3');
53 shouldBe("testReplace(/x/, true); re.lastIndex", '3');
55 replacer = function() { "use strict"; return ++re
.lastIndex
; };
56 shouldBe("testReplace(/x/g, false)", '"01122"');
57 shouldThrow("testReplace(/x/g, true)");
58 shouldBe("testReplace(/x/, false)", '"041x2"');
59 shouldThrow("testReplace(/x/, true)");
60 shouldBe("testReplace(/x/g, false); re.lastIndex", '2');
61 shouldThrow("testReplace(/x/g, true); re.lastIndex");
62 shouldBe("testReplace(/x/, false); re.lastIndex", '4');
63 shouldThrow("testReplace(/x/, true); re.lastIndex");
65 var replacerCalled
= false;
66 replacer = function() { replacerCalled
= true; };
67 shouldBeTrue("try { testReplace(/x/g, false); throw 0; } catch (e) { }; replacerCalled;");
68 var replacerCalled
= false;
69 replacer = function() { replacerCalled
= true; };
70 shouldBeFalse("try { testReplace(/x/g, true); throw 0; } catch (e) { }; replacerCalled;");