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 | 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. |
11 assertThrows("/(?<:a>a)/u"); // Name starting with invalid char. | 11 assertThrows("/(?<:a>a)/u"); // Name starting with invalid char. |
12 assertThrows("/(?<a:>a)/u"); // Name containing with invalid char. | 12 assertThrows("/(?<a:>a)/u"); // Name containing with invalid char. |
13 assertThrows("/(?<a>a)(?<a>a)/u"); // Duplicate name. | 13 assertThrows("/(?<a>a)(?<a>a)/u"); // Duplicate name. |
14 assertThrows("/(?<a>a)(?<b>b)(?<a>a)/u"); // Duplicate name. | 14 assertThrows("/(?<a>a)(?<b>b)(?<a>a)/u"); // Duplicate name. |
15 assertThrows("/\\k<a>/u"); // Invalid reference. | 15 assertThrows("/\\k<a>/u"); // Invalid reference. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 "baba".match(/(.)(?<a>a)(?<b>\1)(\2)/u)); | 60 "baba".match(/(.)(?<a>a)(?<b>\1)(\2)/u)); |
61 assertEquals(["<a", "<"], "<a".match(/(?<lt><)a/u)); | 61 assertEquals(["<a", "<"], "<a".match(/(?<lt><)a/u)); |
62 assertEquals([">a", ">"], ">a".match(/(?<gt>>)a/u)); | 62 assertEquals([">a", ">"], ">a".match(/(?<gt>>)a/u)); |
63 | 63 |
64 // Named references. | 64 // Named references. |
65 assertEquals(["bab", "b"], "bab".match(/(?<b>.).\k<b>/u)); | 65 assertEquals(["bab", "b"], "bab".match(/(?<b>.).\k<b>/u)); |
66 assertNull("baa".match(/(?<b>.).\k<b>/u)); | 66 assertNull("baa".match(/(?<b>.).\k<b>/u)); |
67 | 67 |
68 // Nested groups. | 68 // Nested groups. |
69 assertEquals(["bab", "bab", "ab", "b"], "bab".match(/(?<a>.(?<b>.(?<c>.)))/u)); | 69 assertEquals(["bab", "bab", "ab", "b"], "bab".match(/(?<a>.(?<b>.(?<c>.)))/u)); |
| 70 assertEquals({a: "bab", b: "ab", c: "b"}, |
| 71 "bab".match(/(?<a>.(?<b>.(?<c>.)))/u).groups); |
70 | 72 |
71 // Reference inside group. | 73 // Reference inside group. |
72 assertEquals(["bab", "b"], "bab".match(/(?<a>\k<a>\w)../u)); | 74 assertEquals(["bab", "b"], "bab".match(/(?<a>\k<a>\w)../u)); |
| 75 assertEquals({a: "b"}, "bab".match(/(?<a>\k<a>\w)../u).groups); |
73 | 76 |
74 // Reference before group. | 77 // Reference before group. |
75 assertEquals(["bab", "b"], "bab".match(/\k<a>(?<a>b)\w\k<a>/u)); | 78 assertEquals(["bab", "b"], "bab".match(/\k<a>(?<a>b)\w\k<a>/u)); |
| 79 assertEquals({a: "b"}, "bab".match(/\k<a>(?<a>b)\w\k<a>/u).groups); |
76 assertEquals(["bab", "b", "a"], "bab".match(/(?<b>b)\k<a>(?<a>a)\k<b>/u)); | 80 assertEquals(["bab", "b", "a"], "bab".match(/(?<b>b)\k<a>(?<a>a)\k<b>/u)); |
| 81 assertEquals({a: "a", b: "b"}, |
| 82 "bab".match(/(?<b>b)\k<a>(?<a>a)\k<b>/u).groups); |
77 | 83 |
78 // Reference properties. | 84 // Reference properties. |
79 assertEquals("a", /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.a); | 85 assertEquals("a", /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.a); |
80 assertEquals("b", /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.b); | 86 assertEquals("b", /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.b); |
81 assertEquals(undefined, /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.c); | 87 assertEquals(undefined, /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.c); |
82 assertEquals(undefined, /(?<a>a)(?<b>b)\k<a>|(?<c>c)/u.exec("aba").groups.c); | 88 assertEquals(undefined, /(?<a>a)(?<b>b)\k<a>|(?<c>c)/u.exec("aba").groups.c); |
83 | 89 |
84 // Unicode names. | 90 // Unicode names. |
85 assertEquals("a", /(?<π>a)/u.exec("bab").groups.π); | 91 assertEquals("a", /(?<π>a)/u.exec("bab").groups.π); |
86 assertEquals("a", /(?<\u{03C0}>a)/u.exec("bab").groups.\u03C0); | 92 assertEquals("a", /(?<\u{03C0}>a)/u.exec("bab").groups.\u03C0); |
87 assertEquals("a", /(?<$>a)/u.exec("bab").groups.$); | 93 assertEquals("a", /(?<$>a)/u.exec("bab").groups.$); |
88 assertEquals("a", /(?<_>a)/u.exec("bab").groups._); | 94 assertEquals("a", /(?<_>a)/u.exec("bab").groups._); |
89 assertEquals("a", /(?<$𐒤>a)/u.exec("bab").groups.$𐒤); | 95 assertEquals("a", /(?<$𐒤>a)/u.exec("bab").groups.$𐒤); |
90 assertEquals("a", /(?<_\u200C>a)/u.exec("bab").groups._\u200C); | 96 assertEquals("a", /(?<_\u200C>a)/u.exec("bab").groups._\u200C); |
91 assertEquals("a", /(?<_\u200D>a)/u.exec("bab").groups._\u200D); | 97 assertEquals("a", /(?<_\u200D>a)/u.exec("bab").groups._\u200D); |
92 assertEquals("a", /(?<ಠ_ಠ>a)/u.exec("bab").groups.ಠ_ಠ); | 98 assertEquals("a", /(?<ಠ_ಠ>a)/u.exec("bab").groups.ಠ_ಠ); |
93 assertThrows('/(?<❤>a)/u', SyntaxError); | 99 assertThrows('/(?<❤>a)/u', SyntaxError); |
94 assertThrows('/(?<𐒤>a)/u', SyntaxError); // ID_Continue but not ID_Start. | 100 assertThrows('/(?<𐒤>a)/u', SyntaxError); // ID_Continue but not ID_Start. |
95 | 101 |
| 102 // Interaction with lookbehind assertions. |
| 103 assertEquals(["f", "c"], "abcdef".match(/(?<=(?<a>\w){3})f/u)); |
| 104 assertEquals({a: "c"}, "abcdef".match(/(?<=(?<a>\w){3})f/u).groups); |
| 105 assertEquals({a: "b"}, "abcdef".match(/(?<=(?<a>\w){4})f/u).groups); |
| 106 assertEquals({a: "a"}, "abcdef".match(/(?<=(?<a>\w)+)f/u).groups); |
| 107 assertNull("abcdef".match(/(?<=(?<a>\w){6})f/u)); |
| 108 |
| 109 assertEquals(["f", ""], "abcdef".match(/((?<=\w{3}))f/u)); |
| 110 assertEquals(["f", ""], "abcdef".match(/(?<a>(?<=\w{3}))f/u)); |
| 111 |
| 112 assertEquals(["f", undefined], "abcdef".match(/(?<!(?<a>\d){3})f/u)); |
| 113 assertNull("abcdef".match(/(?<!(?<a>\D){3})f/u)); |
| 114 |
| 115 assertEquals(["f", undefined], "abcdef".match(/(?<!(?<a>\D){3})f|f/u)); |
| 116 assertEquals(["f", undefined], "abcdef".match(/(?<a>(?<!\D{3}))f|f/u)); |
| 117 |
| 118 // Properties created on result.groups. |
| 119 assertEquals(["fst", "snd"], |
| 120 Object.getOwnPropertyNames( |
| 121 /(?<fst>.)|(?<snd>.)/u.exec("abcd").groups)); |
| 122 |
96 // The '__proto__' property on the groups object. | 123 // The '__proto__' property on the groups object. |
97 assertEquals(undefined, /(?<a>.)/u.exec("a").groups.__proto__); | 124 assertEquals(undefined, /(?<a>.)/u.exec("a").groups.__proto__); |
98 assertEquals("a", /(?<__proto__>a)/u.exec("a").groups.__proto__); | 125 assertEquals("a", /(?<__proto__>a)/u.exec("a").groups.__proto__); |
99 | 126 |
100 // @@replace with a callable replacement argument (no named captures). | 127 // @@replace with a callable replacement argument (no named captures). |
101 { | 128 { |
102 let result = "abcd".replace(/(.)(.)/u, (match, fst, snd, offset, str) => { | 129 let result = "abcd".replace(/(.)(.)/u, (match, fst, snd, offset, str) => { |
103 assertEquals("ab", match); | 130 assertEquals("ab", match); |
104 assertEquals("a", fst); | 131 assertEquals("a", fst); |
105 assertEquals("b", snd); | 132 assertEquals("b", snd); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 // named captures). | 303 // named captures). |
277 { | 304 { |
278 let re = toSlowMode(/(?<fst>.)(?<snd>.)/u); | 305 let re = toSlowMode(/(?<fst>.)(?<snd>.)/u); |
279 assertEquals("bacd", "abcd".replace(re, "$<snd>$<fst>")); | 306 assertEquals("bacd", "abcd".replace(re, "$<snd>$<fst>")); |
280 assertEquals("bacd", "abcd".replace(re, "$2$1")); | 307 assertEquals("bacd", "abcd".replace(re, "$2$1")); |
281 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); | 308 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); |
282 assertEquals("cd", "abcd".replace(re, "$<42$1>")); | 309 assertEquals("cd", "abcd".replace(re, "$<42$1>")); |
283 assertEquals("cd", "abcd".replace(re, "$<thd>")); | 310 assertEquals("cd", "abcd".replace(re, "$<thd>")); |
284 assertEquals("cd", "abcd".replace(re, "$<$1>")); | 311 assertEquals("cd", "abcd".replace(re, "$<$1>")); |
285 } | 312 } |
OLD | NEW |