OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 assertEquals("x", m, "replace(/(x)(?=(.))/g,func(m,..))"); | 156 assertEquals("x", m, "replace(/(x)(?=(.))/g,func(m,..))"); |
157 assertEquals("x", c1, "replace(/(x)(?=(.))/g,func(..,c1,..))"); | 157 assertEquals("x", c1, "replace(/(x)(?=(.))/g,func(..,c1,..))"); |
158 assertEquals(["a","b","c"][ctr], c2, "replace(/(x)(?=(.))/g,func(..,c2,..))"); | 158 assertEquals(["a","b","c"][ctr], c2, "replace(/(x)(?=(.))/g,func(..,c2,..))"); |
159 assertEquals(ctr * 2, i, "replace(/(x)(?=(.))/g,func(..,i,..))"); | 159 assertEquals(ctr * 2, i, "replace(/(x)(?=(.))/g,func(..,i,..))"); |
160 assertEquals(short, s, "replace(/(x)(?=(.))/g,func(..,s))"); | 160 assertEquals(short, s, "replace(/(x)(?=(.))/g,func(..,s))"); |
161 return String(ctr++); | 161 return String(ctr++); |
162 }); | 162 }); |
163 assertEquals(3, ctr, "replace(/x/g,func) num-match"); | 163 assertEquals(3, ctr, "replace(/x/g,func) num-match"); |
164 | 164 |
165 | 165 |
| 166 replaceTest("ABCD", "abcd", /(.)/g, function r(m, c1, i, s) { |
| 167 assertEquals("d", RegExp.lastMatch); |
| 168 assertEquals("d", RegExp.$1); |
| 169 assertEquals("abc", RegExp.leftContext); |
| 170 return m.toUpperCase(); |
| 171 }); |
| 172 |
| 173 |
| 174 var long = ""; |
| 175 while (long.length < 0x2000) { |
| 176 long += String.fromCharCode(65 + Math.random() * 26); |
| 177 } |
| 178 |
| 179 for (var i = 0; i < 3; i++) { |
| 180 replaceTest(long.toLowerCase(), long, /(..)/g, function r(m, c1, i, s) { |
| 181 var expected = long.substring(0x1ffe, 0x2000); |
| 182 assertEquals(expected, RegExp.lastMatch); |
| 183 assertEquals(expected, RegExp.$1); |
| 184 assertEquals(long.substring(0, 0x1ffe), RegExp.leftContext); |
| 185 return m.toLowerCase(); |
| 186 }); |
| 187 } |
| 188 |
| 189 |
166 // Test special cases of replacement parts longer than 1<<11. | 190 // Test special cases of replacement parts longer than 1<<11. |
167 var longstring = "xyzzy"; | 191 var longstring = "xyzzy"; |
168 longstring = longstring + longstring; | 192 longstring = longstring + longstring; |
169 longstring = longstring + longstring; | 193 longstring = longstring + longstring; |
170 longstring = longstring + longstring; | 194 longstring = longstring + longstring; |
171 longstring = longstring + longstring; | 195 longstring = longstring + longstring; |
172 longstring = longstring + longstring; | 196 longstring = longstring + longstring; |
173 longstring = longstring + longstring; | 197 longstring = longstring + longstring; |
174 longstring = longstring + longstring; | 198 longstring = longstring + longstring; |
175 longstring = longstring + longstring; | 199 longstring = longstring + longstring; |
(...skipping 11 matching lines...) Expand all Loading... |
187 replaceTest("string 42", "string x", /[xy]/, function() { return 42; }); | 211 replaceTest("string 42", "string x", /[xy]/, function() { return 42; }); |
188 replaceTest("string true", "string x", /x/g, function() { return true; }); | 212 replaceTest("string true", "string x", /x/g, function() { return true; }); |
189 replaceTest("string null", "string x", /x/g, function() { return null; }); | 213 replaceTest("string null", "string x", /x/g, function() { return null; }); |
190 replaceTest("string undefined", "string x", /x/g, function() { return undefined;
}); | 214 replaceTest("string undefined", "string x", /x/g, function() { return undefined;
}); |
191 | 215 |
192 replaceTest("aundefinedbundefinedcundefined", | 216 replaceTest("aundefinedbundefinedcundefined", |
193 "abc", /(.)|(.)/g, function(m, m1, m2, i, s) { return m1+m2; }); | 217 "abc", /(.)|(.)/g, function(m, m1, m2, i, s) { return m1+m2; }); |
194 | 218 |
195 // Test nested calls to replace, including that it sets RegExp.$& correctly. | 219 // Test nested calls to replace, including that it sets RegExp.$& correctly. |
196 | 220 |
197 function replacer(m,i,s) { | |
198 assertEquals(m,RegExp['$&']); | |
199 return "[" + RegExp['$&'] + "-" | |
200 + m.replace(/./g,"$&$&") + "-" | |
201 + m.replace(/./g,function() { return RegExp['$&']; }) | |
202 + "-" + RegExp['$&'] + "]"; | |
203 } | |
204 | |
205 replaceTest("[ab-aabb-ab-b][az-aazz-az-z]", | |
206 "abaz", /a./g, replacer); | |
207 | |
208 replaceTest("[ab-aabb-ab-b][az-aazz-az-z]", | |
209 "abaz", /a(.)/g, replacer); | |
210 | |
211 var str = 'She sells seashells by the seashore.'; | 221 var str = 'She sells seashells by the seashore.'; |
212 var re = /sh/g; | 222 var re = /sh/g; |
213 assertEquals('She sells sea$schells by the sea$schore.', | 223 assertEquals('She sells sea$schells by the sea$schore.', |
214 str.replace(re,"$$" + 'sch')) | 224 str.replace(re,"$$" + 'sch')) |
215 | 225 |
216 | 226 |
217 var replace_obj = { length: 0, toString: function() { return "x"; }}; | 227 var replace_obj = { length: 0, toString: function() { return "x"; }}; |
218 assertEquals("axc", "abc".replace(/b/, replace_obj)); | 228 assertEquals("axc", "abc".replace(/b/, replace_obj)); |
219 assertEquals("axc", "abc".replace(/b/g, replace_obj)); | 229 assertEquals("axc", "abc".replace(/b/g, replace_obj)); |
220 | 230 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 // followed by "0", $61 as $6, followed by "1" and so on. | 276 // followed by "0", $61 as $6, followed by "1" and so on. |
267 var tail = subject.substr(59); | 277 var tail = subject.substr(59); |
268 for (var i = 60; i < 100; i++) { | 278 for (var i = 60; i < 100; i++) { |
269 assertEquals(String.fromCharCode(i / 10 + 23) + (i % 10) + tail, | 279 assertEquals(String.fromCharCode(i / 10 + 23) + (i % 10) + tail, |
270 subject.replace(re, "$" + i)); | 280 subject.replace(re, "$" + i)); |
271 } | 281 } |
272 } | 282 } |
273 | 283 |
274 testIndices59(new RegExp(regexp59pattern)); | 284 testIndices59(new RegExp(regexp59pattern)); |
275 testIndices59(new RegExp(regexp59pattern, "g")); | 285 testIndices59(new RegExp(regexp59pattern, "g")); |
OLD | NEW |