• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15assert ("abcabc".replace("bc", ":") === "a:abc");
16assert ("hello".replace("", ":") === ":hello");
17assert ("hello".replace("h", "") === "ello");
18assert ("".replace("", "h") === "h");
19
20assert ("xabcxabcx".replace (/abc/g, "[$&][$`][$']") === "x[abc][x][xabcx]x[abc][xabcx][x]x");
21assert ("abc".replace (/a(b)c|d()/, "[$1][$01][$2][$02][$99][$123][$012]") === "[b][b][][][$99][b23][b2]");
22assert ("abc".replace("abc", "$x$$5$0$00$") === "$x$5$0$00$");
23
24assert ("#x#".replace("x", "$1") === "#$1#");
25assert ("#x#".replace(/(x)/, "$1$2") === "#x$2#");
26assert ("#x#".replace(/(x)/, "$01$02$11$20") === "#x$02x1$20#");
27assert ("#xy#".replace(/(x)((((((((((y))))))))))/, "$07|$20|$11|$12|$110|$99|$011") === "#y|y0|y|x2|y0|y9|x1#");
28assert ("#xy#".replace(/(x)((((((((y))))))))/, "$00|$01|$011|$090|$10|$99") === "#$00|x|x1|y0|x0|y9#");
29
30assert ("a true true story".replace(true) === "a undefined true story");
31assert ("1234".replace(23, 32) === "1324");
32
33assert ("abcabc".replace(/bc/, ":") === "a:abc");
34assert ("axbcxx".replace(/x*/g, ":") === ":a::b:c::");
35
36assert ("".replace(/|/g,"஻") === "஻");
37assert ("஻BB8B@abXde^".replace(/a/g,"$஻Bce((/a%") === "஻BB8B@$஻Bce((/a%bXde^");
38assert ("abcab".replace(/a/g,"˙Ł$Đ") === "˙Ł$Đbc˙Ł$Đb");
39assert ("˙Ł$Đbc˙Ł$Đb".replace("Ł$","ab") === "˙abĐbc˙Ł$Đb");
40
41assert (String.prototype.replace.call (12321, /2/g, ".") === "1.3.1");
42
43try
44{
45  String.prototype.replace.call (null, "u", ".");
46  assert (false);
47}
48catch (e)
49{
50  assert (e instanceof TypeError);
51}
52
53assert ("98765".replace(76, function () { return {}; }) === "98[object Object]5");
54
55function concat_arguments()
56{
57  var str = "";
58  for (var i = 0; i < arguments.length; i++)
59  {
60    str += "[" + arguments[i] + "]";
61  }
62  return str;
63}
64
65assert ("abcdabcd".replace("cd", concat_arguments) === "ab[cd][2][abcdabcd]abcd");
66assert ("abcdef".replace (/a((b)c)|d()/, concat_arguments) === "[abc][bc][b][undefined][0][abcdef]def");
67
68try
69{
70  "x".replace("x", function() { throw "MyError"; });
71  assert (false);
72}
73catch (e)
74{
75  assert (e === "MyError");
76}
77
78assert ("\ud801\udc00".replace("\ud801", "#") === "#\udc00");
79assert ("\ud801\udc00".replace("\udc00", "#") === "\ud801#");
80
81var global = this;
82
83function case1()
84{
85  assert(this === global);
86  return "y";
87}
88
89function case2()
90{
91  "use strict";
92  assert(this === undefined);
93  return "y";
94}
95
96assert ("x".replace("x", case1) === "y");
97assert ("x".replace("x", case2) === "y");
98
99var regexp = /r/g;
100
101Object.defineProperty(regexp, "lastIndex", {
102  configurable : false,
103  enumerable : false,
104  value : 0,
105  writable : false
106});
107
108try {
109  "r".replace (regexp, "x");
110  assert (false);
111} catch (e) {
112  assert (e instanceof TypeError);
113}
114
115try {
116  "str".replace ({toString: function () {throw "abrupt search toString"}}, "");
117  assert (false);
118} catch (e) {
119  assert (e === "abrupt search toString");
120}
121
122try {
123  "str".replace ("str", {toString: function () {throw "abrupt search toString"}});
124  assert (false);
125} catch (e) {
126  assert (e === "abrupt search toString");
127}
128
129try {
130  "str".replace ("str", function () {return {toString: function () {throw "abrupt replacer toString"}}});
131  assert (false);
132} catch (e) {
133  assert (e === "abrupt replacer toString");
134}
135