OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-regexp-named-captures --harmony-regexp-lookbehind | 5 // Flags: --harmony-regexp-named-captures --harmony-regexp-lookbehind |
6 | 6 |
7 // Malformed named captures. | 7 // Malformed named captures. |
8 assertThrows("/(?<>a)/u"); // Empty name. | 8 assertThrows("/(?<>a)/u"); // Empty name. |
9 assertThrows("/(?<aa)/u"); // Unterminated name. | 9 assertThrows("/(?<aa)/u"); // Unterminated name. |
10 assertThrows("/(?<42a>a)/u"); // Name starting with digits. | 10 assertThrows("/(?<42a>a)/u"); // Name starting with digits. |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 }); | 256 }); |
257 assertEquals("bacd", result); | 257 assertEquals("bacd", result); |
258 | 258 |
259 assertEquals("undefinedbcd", | 259 assertEquals("undefinedbcd", |
260 "abcd".replace(toSlowMode(/(?<fst>.)|(?<snd>.)/u), | 260 "abcd".replace(toSlowMode(/(?<fst>.)|(?<snd>.)/u), |
261 (match, fst, snd, offset, str, groups) => groups.snd)); | 261 (match, fst, snd, offset, str, groups) => groups.snd)); |
262 } | 262 } |
263 | 263 |
264 // @@replace with a string replacement argument (no named captures). | 264 // @@replace with a string replacement argument (no named captures). |
265 { | 265 { |
266 let re = /(.)(.)/u; | 266 let re = /(.)(.)|(x)/u; |
267 assertEquals("$<snd>$<fst>cd", "abcd".replace(re, "$<snd>$<fst>")); | 267 assertEquals("$<snd>$<fst>cd", "abcd".replace(re, "$<snd>$<fst>")); |
268 assertEquals("bacd", "abcd".replace(re, "$2$1")); | 268 assertEquals("bacd", "abcd".replace(re, "$2$1")); |
| 269 assertEquals("cd", "abcd".replace(re, "$3")); |
269 assertEquals("$<sndcd", "abcd".replace(re, "$<snd")); | 270 assertEquals("$<sndcd", "abcd".replace(re, "$<snd")); |
270 assertEquals("$<42a>cd", "abcd".replace(re, "$<42$1>")); | 271 assertEquals("$<42a>cd", "abcd".replace(re, "$<42$1>")); |
271 assertEquals("$<thd>cd", "abcd".replace(re, "$<thd>")); | 272 assertEquals("$<fth>cd", "abcd".replace(re, "$<fth>")); |
272 assertEquals("$<a>cd", "abcd".replace(re, "$<$1>")); | 273 assertEquals("$<a>cd", "abcd".replace(re, "$<$1>")); |
273 } | 274 } |
274 | 275 |
275 // @@replace with a string replacement argument (global, named captures). | 276 // @@replace with a string replacement argument (global, named captures). |
276 { | 277 { |
277 let re = /(?<fst>.)(?<snd>.)/gu; | 278 let re = /(?<fst>.)(?<snd>.)|(?<thd>x)/gu; |
278 assertEquals("badc", "abcd".replace(re, "$<snd>$<fst>")); | 279 assertEquals("badc", "abcd".replace(re, "$<snd>$<fst>")); |
279 assertEquals("badc", "abcd".replace(re, "$2$1")); | 280 assertEquals("badc", "abcd".replace(re, "$2$1")); |
| 281 assertEquals("", "abcd".replace(re, "$<thd>")); |
280 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); | 282 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); |
281 assertEquals("", "abcd".replace(re, "$<42$1>")); | 283 assertThrows(() => "abcd".replace(re, "$<42$1>"), SyntaxError); |
282 assertEquals("", "abcd".replace(re, "$<thd>")); | 284 assertThrows(() => "abcd".replace(re, "$<fth>"), SyntaxError); |
283 assertEquals("", "abcd".replace(re, "$<$1>")); | 285 assertThrows(() => "abcd".replace(re, "$<$1>"), SyntaxError); |
284 } | 286 } |
285 | 287 |
286 // @@replace with a string replacement argument (non-global, named captures). | 288 // @@replace with a string replacement argument (non-global, named captures). |
287 { | 289 { |
288 let re = /(?<fst>.)(?<snd>.)/u; | 290 let re = /(?<fst>.)(?<snd>.)|(?<thd>x)/u; |
289 assertEquals("bacd", "abcd".replace(re, "$<snd>$<fst>")); | 291 assertEquals("bacd", "abcd".replace(re, "$<snd>$<fst>")); |
290 assertEquals("bacd", "abcd".replace(re, "$2$1")); | 292 assertEquals("bacd", "abcd".replace(re, "$2$1")); |
| 293 assertEquals("cd", "abcd".replace(re, "$<thd>")); |
291 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); | 294 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); |
292 assertEquals("cd", "abcd".replace(re, "$<42$1>")); | 295 assertThrows(() => "abcd".replace(re, "$<42$1>"), SyntaxError); |
293 assertEquals("cd", "abcd".replace(re, "$<thd>")); | 296 assertThrows(() => "abcd".replace(re, "$<fth>"), SyntaxError); |
294 assertEquals("cd", "abcd".replace(re, "$<$1>")); | 297 assertThrows(() => "abcd".replace(re, "$<$1>"), SyntaxError); |
295 } | 298 } |
296 | 299 |
297 // @@replace with a string replacement argument (slow, global, named captures). | 300 // @@replace with a string replacement argument (slow, global, named captures). |
298 { | 301 { |
299 let re = toSlowMode(/(?<fst>.)(?<snd>.)/gu); | 302 let re = toSlowMode(/(?<fst>.)(?<snd>.)|(?<thd>x)/gu); |
300 assertEquals("badc", "abcd".replace(re, "$<snd>$<fst>")); | 303 assertEquals("badc", "abcd".replace(re, "$<snd>$<fst>")); |
301 assertEquals("badc", "abcd".replace(re, "$2$1")); | 304 assertEquals("badc", "abcd".replace(re, "$2$1")); |
| 305 assertEquals("", "abcd".replace(re, "$<thd>")); |
302 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); | 306 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); |
303 assertEquals("", "abcd".replace(re, "$<42$1>")); | 307 assertThrows(() => "abcd".replace(re, "$<42$1>"), SyntaxError); |
304 assertEquals("", "abcd".replace(re, "$<thd>")); | 308 assertThrows(() => "abcd".replace(re, "$<fth>"), SyntaxError); |
305 assertEquals("", "abcd".replace(re, "$<$1>")); | 309 assertThrows(() => "abcd".replace(re, "$<$1>"), SyntaxError); |
306 } | 310 } |
307 | 311 |
308 // @@replace with a string replacement argument (slow, non-global, | 312 // @@replace with a string replacement argument (slow, non-global, |
309 // named captures). | 313 // named captures). |
310 { | 314 { |
311 let re = toSlowMode(/(?<fst>.)(?<snd>.)/u); | 315 let re = toSlowMode(/(?<fst>.)(?<snd>.)|(?<thd>x)/u); |
312 assertEquals("bacd", "abcd".replace(re, "$<snd>$<fst>")); | 316 assertEquals("bacd", "abcd".replace(re, "$<snd>$<fst>")); |
313 assertEquals("bacd", "abcd".replace(re, "$2$1")); | 317 assertEquals("bacd", "abcd".replace(re, "$2$1")); |
| 318 assertEquals("cd", "abcd".replace(re, "$<thd>")); |
314 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); | 319 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); |
315 assertEquals("cd", "abcd".replace(re, "$<42$1>")); | 320 assertThrows(() => "abcd".replace(re, "$<42$1>"), SyntaxError); |
316 assertEquals("cd", "abcd".replace(re, "$<thd>")); | 321 assertThrows(() => "abcd".replace(re, "$<fth>"), SyntaxError); |
317 assertEquals("cd", "abcd".replace(re, "$<$1>")); | 322 assertThrows(() => "abcd".replace(re, "$<$1>"), SyntaxError); |
318 } | 323 } |
OLD | NEW |