blob: 46f656932794166f4407e28f567c6d5a8c4b9377 [file] [log] [blame]
msaboff@apple.com5e9b0652016-03-02 00:39:01 +00001description(
2'Test for unicode regular expression processing'
3);
4
5// Test \u{} escapes in a regular expression
msaboff@apple.com5bc39062016-03-04 01:24:28 +00006shouldBe('"a".match(/\u{61}/u)[0].length', '1');
7shouldBe('"a".match(/\u{41}/ui)[0].length', '1');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +00008shouldBe('"a".match(/\u{061}/u)[0].length', '1');
9shouldBe('"a".match(/\u{041}/iu)[0].length', '1');
msaboff@apple.com5bc39062016-03-04 01:24:28 +000010shouldBe('"\u{212}".match(/\u{212}/u)[0].length', '1');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000011shouldBe('"\u{212}".match(/\u{0212}/u)[0].length', '1');
msaboff@apple.com5bc39062016-03-04 01:24:28 +000012shouldBe('"\u{1234}".match(/\u{1234}/u)[0].length', '1');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000013shouldBe('"\u{1234}".match(/\u{01234}/u)[0].length', '1');
msaboff@apple.com5bc39062016-03-04 01:24:28 +000014shouldBe('"\u{2abc}".match(/\u{2abc}/u)[0].length', '1');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000015shouldBe('"\u{03fed}".match(/\u{03fed}/u)[0].length', '1');
16shouldBe('"\u{12345}".match(/\u{12345}/u)[0].length', '2');
17shouldBe('"\u{12345}".match(/\u{012345}/u)[0].length', '2');
msaboff@apple.com5bc39062016-03-04 01:24:28 +000018shouldBe('"\u{1d306}".match(/\u{1d306}/u)[0].length', '2');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000019shouldBeTrue('/\u{1044f}/u.test("\ud801\udc4f")');
20shouldBeTrue('/\ud801\udc4f/u.test("\u{1044f}")');
21
22// Test basic unicode flag processing
23shouldBe('"\u{1d306}".match(/\u{1d306}/u)[0].length', '2');
24shouldBeTrue('/(\u{10000}|\u{10400}|\u{10429})/u.test("\u{10400}")');
25shouldBe('"\u{10123}".match(/a|\u{10123}|b/u)[0].length', '2');
26shouldBe('"b".match(/a|\u{10123}|b/u)[0].length', '1');
27shouldBeFalse('/(?:a|\u{10123}|b)x/u.test("\u{10123}")');
28shouldBeTrue('/(?:a|\u{10123}|b)x/u.test("\u{10123}x")');
29shouldBeFalse('/(?:a|\u{10123}|b)x/u.test("b")');
30shouldBeTrue('/(?:a|\u{10123}|b)x/u.test("bx")');
31shouldBe('"a\u{10123}x".match(/a\u{10123}b|a\u{10123}x/u)[0].length', '4');
32
33// Test unicode flag with ignore case
34shouldBeTrue('/(\u{10000}|\u{10400}|\u{10429})x/ui.test("\u{10400}x")');
35shouldBeTrue('/(\u{10000}|\u{10400}|\u{10429})x/ui.test("\u{10429}x")');
36shouldBeTrue('/(\u{10000}|\u{10400}|\u{10429})x/ui.test("\u{10401}x")');
37shouldBeTrue('/(\u{10000}|\u{10400}|\u{10429})x/ui.test("\u{10428}x")');
38shouldBe('"\u{10429}".match(/a|\u{10401}|b/iu)[0].length', '2');
39shouldBe('"B".match(/a|\u{10123}|b/iu)[0].length', '1');
40shouldBeFalse('/(?:A|\u{10123}|b)x/iu.test("\u{10123}")');
41shouldBeTrue('/(?:A|\u{10123}|b)x/iu.test("\u{10123}x")');
42shouldBeFalse('/(?:A|\u{10123}|b)x/iu.test("b")');
43shouldBeTrue('/(?:A|\u{10123}|b)x/iu.test("bx")');
44shouldBe('"a\u{10123}X".match(/a\u{10123}b|a\u{10123}x/iu)[0].length', '4');
45shouldBe('"\u0164x".match(/\u0165x/iu)[0].length', '2');
msaboff@apple.com330ae112016-04-14 00:47:40 +000046shouldBeTrue('/\\w/iu.test("\u017f")');
47shouldBeTrue('/\\w/iu.test("\u212a")');
msaboff@apple.com080c6092016-06-27 17:38:55 +000048shouldBeFalse('/\\W/iu.test("\u017f")');
49shouldBeFalse('/\\W/iu.test("\u212a")');
msaboff@apple.com330ae112016-04-14 00:47:40 +000050shouldBeTrue('/[\\w\\d]/iu.test("\u017f")');
51shouldBeTrue('/[\\w\\d]/iu.test("\u212a")');
52shouldBeFalse('/[^\\w\\d]/iu.test("\u017f")');
53shouldBeFalse('/[^\\w\\d]/iu.test("\u212a")');
msaboff@apple.com080c6092016-06-27 17:38:55 +000054shouldBeFalse('/[\\W\\d]/iu.test("\u017f")');
55shouldBeFalse('/[\\W\\d]/iu.test("\u212a")');
56shouldBeTrue('/[^\\W\\d]/iu.test("\u017f")');
57shouldBeTrue('/[^\\W\\d]/iu.test("\u212a")');
msaboff@apple.com330ae112016-04-14 00:47:40 +000058shouldBeTrue('/\\w/iu.test("S")');
59shouldBeTrue('/\\w/iu.test("K")');
msaboff@apple.com080c6092016-06-27 17:38:55 +000060shouldBeFalse('/\\W/iu.test("S")');
61shouldBeFalse('/\\W/iu.test("K")');
msaboff@apple.com330ae112016-04-14 00:47:40 +000062shouldBeTrue('/[\\w\\d]/iu.test("S")');
63shouldBeTrue('/[\\w\\d]/iu.test("K")');
64shouldBeFalse('/[^\\w\\d]/iu.test("S")');
65shouldBeFalse('/[^\\w\\d]/iu.test("K")');
msaboff@apple.com080c6092016-06-27 17:38:55 +000066shouldBeFalse('/[\\W\\d]/iu.test("S")');
67shouldBeFalse('/[\\W\\d]/iu.test("K")');
68shouldBeTrue('/[^\\W\\d]/iu.test("S")');
69shouldBeTrue('/[^\\W\\d]/iu.test("K")');
70shouldBe('"Gras\u017foden is old German for grass".match(/.*?\\Bs\\u017foden/iu)[0]', '"Gras\u017foden"');
71shouldBe('"Gras\u017foden is old German for grass".match(/.*?\\B\\u017foden/iu)[0]', '"Gras\u017foden"');
72shouldBe('"Gras\u017foden is old German for grass".match(/.*?\\Boden/iu)[0]', '"Gras\u017foden"');
73shouldBe('"Gras\u017foden is old German for grass".match(/.*?\\Bden/iu)[0]', '"Gras\u017foden"');
74shouldBe('"Water freezes at 273\u212a which is 0C.".split(/\\b\\s/iu)', '["Water","freezes","at","273\u212a","which","is","0C."]');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000075
76// Test . matches with Unicode flag
77shouldBe('"\u{1D306}".match(/^.$/u)[0].length', '2');
78shouldBe('"It is 78\u00B0".match(/.*/u)[0].length', '9');
msaboff@apple.com5bc39062016-03-04 01:24:28 +000079var stringWithDanglingFirstSurrogate = "X\uD801X";
80shouldBe('stringWithDanglingFirstSurrogate.match(/.*/u)[0].length', '3'); // We should match a dangling first surrogate as 1 character
81var stringWithDanglingSecondSurrogate = "X\uDF01X";
82shouldBe('stringWithDanglingSecondSurrogate.match(/.*/u)[0].length', '3'); // We should match a dangling second surrogate as 1 character
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000083
84// Test character classes with unicode characters with and without unicode flag
msaboff@apple.com5bc39062016-03-04 01:24:28 +000085shouldBe('"\u{1d306}".match(/[\uD834\uDF06a]/)[0].length', '1');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000086shouldBe('"\u{1d306}".match(/[a\u{1d306}]/u)[0].length', '2');
87shouldBe('"\u{1d306}".match(/[\u{1d306}a]/u)[0].length', '2');
msaboff@apple.com5bc39062016-03-04 01:24:28 +000088shouldBe('"\u{1d306}".match(/[a-\uD834\uDF06]/)[0].length', '1');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000089shouldBe('"\u{1d306}".match(/[a-\u{1d306}]/u)[0].length', '2');
90
91// Test a character class that is a range from one UTF16 to a Unicode character
92shouldBe('"X".match(/[\u0020-\ud801\udc4f]/u)[0].length', '1');
93shouldBe('"\u1000".match(/[\u0020-\ud801\udc4f]/u)[0].length', '1');
94shouldBe('"\ud801\udc27".match(/[\u0020-\ud801\udc4f]/u)[0].length', '2');
95
msaboff@apple.com5bc39062016-03-04 01:24:28 +000096var re1 = new RegExp("[^\u0020-\uD801\uDC4F]", "u");
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000097shouldBeFalse('re1.test("Z")');
98shouldBeFalse('re1.test("\u{1000}")');
99shouldBeFalse('re1.test("\u{10400}")');
100
101var re2 = new RegExp("[a-z\u{10000}-\u{15000}]", "iu");
102shouldBeTrue('re2.test("A")');
103shouldBeFalse('re2.test("\uffff")');
104shouldBeTrue('re2.test("\u{12345}")');
105
106// Make sure we properly handle dangling surrogates and combined surrogates
107// FIXME: These tests are disabled until https://bugs.webkit.org/show_bug.cgi?id=154863 is fixed
108// shouldBe('/[\u{10c01}\uD803#\uDC01]/u.exec("\u{10c01}").toString()', '"\u{10c01}"');
109// shouldBe('/[\uD803\u{10c01}\uDC01]/u.exec("\u{10c01}").toString()', '"\u{10c01}"');
110// shouldBe('/[\uD803#\uDC01\u{10c01}]/u.exec("\u{10c01}").toString()', '"\u{10c01}"');
111// shouldBe('/[\uD803\uD803\uDC01\uDC01]/u.exec("\u{10c01}").toString()', '"\u{10c01}"');
112// shouldBeNull('/[\u{10c01}\uD803#\uDC01]{2}/u.exec("\u{10c01}")');
113// shouldBeNull('/[\uD803\u{10c01}\uDC01]{2}/u.exec("\u{10c01}")');
114// shouldBeNull('/[\uD803#\uDC01\u{10c01}]{2}/u.exec("\u{10c01}")');
115// shouldBeNull('/[\uD803\uD803\uDC01\uDC01]{2}/u.exec("\u{10c01}")');
116// shouldBe('/\uD803|\uDC01|\u{10c01}/u.exec("\u{10c01}").toString()', '"\u{10c01}"');
117// shouldBe('/\uD803|\uD803\uDC01|\uDC01/u.exec("\u{10c01}").toString()', '"\u{10c01}"');
118// shouldBe('/\uD803|\uDC01|\u{10c01}/u.exec("\u{D803}").toString()', '"\u{D803}"');
119// shouldBe('/\uD803|\uD803\uDC01|\uDC01/u.exec("\u{DC01}").toString()', '"\u{DC01}"');
120// shouldBeNull('/\uD803\u{10c01}/u.exec("\u{10c01}")');
121// shouldBeNull('/\uD803\u{10c01}/u.exec("\uD803")');
122// shouldBe('"\uD803\u{10c01}".match(/\uD803\u{10c01}/u)[0].length', '3');
123
msaboff@apple.comf3669c72016-03-31 00:38:20 +0000124// Check quantified matches
125shouldBeTrue('/\u{1d306}{2}/u.test("\u{1d306}\u{1d306}")');
126shouldBeTrue('/\uD834\uDF06{2}/u.test("\uD834\uDF06\uD834\uDF06")');
msaboff@apple.com6e73b412017-08-22 22:43:08 +0000127shouldBe('"\u{10405}\u{10405}\u{10405}\u{10405}".match(/\u{10405}{3}/u)[0]', '"\u{10405}\u{10405}\u{10405}"');
128shouldBe('"\u{10402}\u{10405}\u{10405}\u{10405}".match(/\u{10405}{3}/u)[0]', '"\u{10405}\u{10405}\u{10405}"');
msaboff@apple.comf3669c72016-03-31 00:38:20 +0000129shouldBe('"\u{10401}\u{10401}\u{10400}".match(/\u{10401}{1,3}/u)[0]', '"\u{10401}\u{10401}"');
130shouldBe('"\u{10401}\u{10429}".match(/\u{10401}{1,3}/iu)[0]', '"\u{10401}\u{10429}"');
131shouldBe('"\u{10401}\u{10429}\u{1042a}\u{10429}".match(/\u{10401}{1,}/iu)[0]', '"\u{10401}\u{10429}"');
132
msaboff@apple.com5e9b0652016-03-02 00:39:01 +0000133// Check back tracking on partial matches
134shouldBe('"\u{10311}\u{10311}\u{10311}".match(/\u{10311}*a|\u{10311}*./u)[0]', '"\u{10311}\u{10311}\u{10311}"');
135shouldBe('"a\u{10311}\u{10311}".match(/a\u{10311}*?$/u)[0]', '"a\u{10311}\u{10311}"');
136shouldBe('"a\u{10311}\u{10311}\u{10311}c".match(/a\u{10311}*cd|a\u{10311}*c/u)[0]', '"a\u{10311}\u{10311}\u{10311}c"');
137shouldBe('"a\u{10311}\u{10311}\u{10311}c".match(/a\u{10311}+cd|a\u{10311}+c/u)[0]', '"a\u{10311}\u{10311}\u{10311}c"');
138shouldBe('"\u{10311}\u{10311}\u{10311}".match(/\u{10311}+?a|\u{10311}+?./u)[0]', '"\u{10311}\u{10311}"');
139shouldBe('"\u{10311}\u{10311}\u{10311}".match(/\u{10311}+?a|\u{10311}+?$/u)[0]', '"\u{10311}\u{10311}\u{10311}"');
140shouldBe('"a\u{10311}\u{10311}\u{10311}c".match(/a\u{10311}*?cd|a\u{10311}*?c/u)[0]', '"a\u{10311}\u{10311}\u{10311}c"');
141shouldBe('"a\u{10311}\u{10311}\u{10311}c".match(/a\u{10311}+?cd|a\u{10311}+?c/u)[0]', '"a\u{10311}\u{10311}\u{10311}c"');
142shouldBe('"\u{10311}\u{10311}\u{10311}".match(/\u{10311}+?a|\u{10311}+?./iu)[0]', '"\u{10311}\u{10311}"');
143shouldBe('"\u{1042a}\u{1042a}\u{10311}".match(/\u{10402}*\u{10200}|\u{10402}*\u{10311}/iu)[0]', '"\u{1042a}\u{1042a}\u{10311}"');
144shouldBe('"\u{1042a}\u{1042a}\u{10311}".match(/\u{10402}+\u{10200}|\u{10402}+\u{10311}/iu)[0]', '"\u{1042a}\u{1042a}\u{10311}"');
145shouldBe('"\u{1042a}\u{1042a}\u{10311}".match(/\u{10402}*?\u{10200}|\u{10402}*?\u{10311}/iu)[0]', '"\u{1042a}\u{1042a}\u{10311}"');
146shouldBe('"\u{1042a}\u{1042a}\u{10311}".match(/\u{10402}+?\u{10200}|\u{10402}+?\u{10311}/iu)[0]', '"\u{1042a}\u{1042a}\u{10311}"');
147shouldBe('"ab\u{10311}c\u{10a01}".match(/abc|ab\u{10311}cd|ab\u{10311}c\u{10a01}d|ab\u{10311}c\u{10a01}/u)[0]', '"ab\u{10311}c\u{10a01}"');
148shouldBe('"ab\u{10428}c\u{10a01}".match(/abc|ab\u{10400}cd|ab\u{10400}c\u{10a01}d|ab\u{10400}c\u{10a01}/iu)[0]', '"ab\u{10428}c\u{10a01}"');
149shouldBeFalse('/abc|ab\u{10400}cd|ab\u{10400}c\u{10a01}d|ab\u{10400}c\u{10a01}/iu.test("qwerty123")');
150shouldBe('"a\u{10428}\u{10428}\u{10428}c".match(/ac|a\u{10400}*cd|a\u{10400}+cd|a\u{10400}+c/iu)[0]', '"a\u{10428}\u{10428}\u{10428}c"');
151shouldBe('"ab\u{10428}\u{10428}\u{10428}c\u{10a01}".match(/abc|ab\u{10400}*cd|ab\u{10400}+c\u{10a01}d|ab\u{10400}+c\u{10a01}/iu)[0]', '"ab\u{10428}\u{10428}\u{10428}c\u{10a01}"');
152shouldBe('"ab\u{10428}\u{10428}\u{10428}".match(/abc|ab\u{10428}*./u)[0]', '"ab\u{10428}\u{10428}\u{10428}"');
153shouldBe('"ab\u{10428}\u{10428}\u{10428}".match(/abc|ab\u{10400}*./iu)[0]', '"ab\u{10428}\u{10428}\u{10428}"');
msaboff@apple.com6094e9a2016-03-24 14:19:37 +0000154shouldBe('"\u{10400}".match(/a*/u)[0].length', '0');
155shouldBe('"\u{10400}".match(/a*/ui)[0].length', '0');
156shouldBe('"\u{10400}".match(/\\d*/u)[0].length', '0');
157shouldBe('"123\u{10400}".match(/\\d*/u)[0]', '"123"');
158shouldBe('"12X3\u{10400}4".match(/\\d{0,1}/ug)', '["1", "2", "", "3", "", "4", ""]');
msaboff@apple.com6e73b412017-08-22 22:43:08 +0000159shouldBe('"\u{10402}\u{10405}\u{10405}\u{10402}\u{10405}\u{10405}\u{10405}".match(/\u{10405}{3}/u)[0]', '"\u{10405}\u{10405}\u{10405}"');
msaboff@apple.come020e962017-08-23 22:24:30 +0000160shouldBe('"a\u{10410}\u{10410}b".match(/a(\u{10410}*?)bc|a(\u{10410}*?)b/ui)[0]', '"a\u{10410}\u{10410}b"');
msaboff@apple.com5e9b0652016-03-02 00:39:01 +0000161
162var re3 = new RegExp("(a\u{10410}*bc)|(a\u{10410}*b)", "u");
163var match3 = "a\u{10410}\u{10410}b".match(re3);
164shouldBe('match3[0]', '"a\u{10410}\u{10410}b"');
165shouldBeUndefined('match3[1]');
166shouldBe('match3[2]', '"a\u{10410}\u{10410}b"');
167
168var re4 = new RegExp("a(\u{10410}*)bc|a(\u{10410}*)b", "ui");
169var match4 = "a\u{10438}\u{10438}b".match(re4);
170shouldBe('match4[0]', '"a\u{10438}\u{10438}b"');
171shouldBeUndefined('match4[1]');
172shouldBe('match4[2]', '"\u{10438}\u{10438}"');
173
174var match5 = "a\u{10412}\u{10412}b\u{10412}\u{10412}".match(/a(\u{10412}*)bc\1|a(\u{10412}*)b\2/u);
175shouldBe('match5[0]', '"a\u{10412}\u{10412}b\u{10412}\u{10412}"');
176shouldBeUndefined('match5[1]');
177shouldBe('match5[2]', '"\u{10412}\u{10412}"');
178
179var match6 = "a\u{10412}\u{10412}b\u{1043a}\u{10412}\u{1043a}".match(/a(\u{1043a}*)bc\1|a(\u{1043a}*)b\2/iu);
180shouldBe('match6[0]', '"a\u{10412}\u{10412}b\u{1043a}\u{10412}"');
181shouldBeUndefined('match6[1]');
182shouldBe('match6[2]', '"\u{10412}\u{10412}"');
183
msaboff@apple.com5bc39062016-03-04 01:24:28 +0000184// Check unicode case insensitive matches
msaboff@apple.com94db89d2016-03-08 18:35:58 +0000185shouldBeTrue('/\u017ftop/ui.test("stop")');
186shouldBeTrue('/stop/ui.test("\u017ftop")');
187shouldBeTrue('/\u212aelvin/ui.test("kelvin")');
188shouldBeTrue('/KELVIN/ui.test("\u212aelvin")');
msaboff@apple.com5bc39062016-03-04 01:24:28 +0000189
190// Verify that without the unicode flag, \u{} doesn't parse to a unicode escapes, but to a counted match of the character 'u'.
191shouldBeTrue('/\\u{1}/.test("u")');
192shouldBeFalse('/\\u{4}/.test("u")');
193shouldBeTrue('/\\u{4}/.test("uuuu")');
194
195// Check that \- escape works in a character class for a unicode pattern
196shouldBe('"800-555-1212".match(/[0-9\\-]*/u)[0].length', '12');
197
msaboff@apple.com6e73b412017-08-22 22:43:08 +0000198// Check that counted Unicode character classes work.
199var re7 = new RegExp("(?:[\u{1f0a1}\u{1f0b1}\u{1f0d1}\u{1f0c1}]{2,4})", "u");
200shouldBe('"\u{1f0a1}\u{1f0d1}\u{1f0b8}\u{1f0c9}\u{1f0da}".match(re7)[0]', '"\u{1f0a1}\u{1f0d1}"');
201shouldBe('"\u{1f0a1}\u{1f0d1}\u{1f0b1}\u{1f0c9}\u{1f0da}".match(re7)[0]', '"\u{1f0a1}\u{1f0d1}\u{1f0b1}"');
202shouldBe('"\u{1f0a1}\u{1f0d1}\u{1f0b1}\u{1f0c1}\u{1f0da}".match(re7)[0]', '"\u{1f0a1}\u{1f0d1}\u{1f0b1}\u{1f0c1}"');
203shouldBe('"\u{1f0a3}\u{1f0d1}\u{1f0b1}\u{1f0c1}\u{1f0da}".match(re7)[0]', '"\u{1f0d1}\u{1f0b1}\u{1f0c1}"');
204shouldBe('"\u{10311}\u{10310}\u{10311}".match(/[\u{10301}\u{10311}]*a|[\u{10310}\u{10311}]*./iu)[0]', '"\u{10311}\u{10310}\u{10311}"');
205shouldBe('"\u{10311}\u{10310}\u{10311}".match(/[\u{10301}\u{10311}]*?a|[\u{10310}\u{10311}]*?./iu)[0]', '"\u{10311}"');
206shouldBe('"\u{10311}\u{10310}\u{10311}".match(/[\u{10301}\u{10311}]+a|[\u{10310}\u{10311}]+./iu)[0]', '"\u{10311}\u{10310}\u{10311}"');
207shouldBe('"\u{10311}\u{10310}\u{10311}".match(/[\u{10301}\u{10311}]+?a|[\u{10310}\u{10311}]+?./iu)[0]', '"\u{10311}\u{10310}"');
208
209var re8 = new RegExp("^([0-9a-z\.]{3,16})\\|\u{041d}\u{0410}\u{0427}\u{0410}\u{0422}\u{042c}", "ui");
210shouldBe('"C83|\u{041d}\u{0410}\u{0427}\u{0410}\u{0422}\u{042c}".match(re8)[0]', '"C83|\u{041d}\u{0410}\u{0427}\u{0410}\u{0422}\u{042c}"');
211shouldBe('"This.Is.16.Chars|\u{041d}\u{0410}\u{0427}\u{0410}\u{0422}\u{042c}".match(re8)[0]', '"This.Is.16.Chars|\u{041d}\u{0410}\u{0427}\u{0410}\u{0422}\u{042c}"');
212
213// Check that unicode characters work with ^ and $ for multiline patterns
214shouldBe('"Testing\\n\u{1234} 1 2 3".match(/^[\u{1000}-\u{100ff}] 1 2 3/um)[0]', '"\u{1234} 1 2 3"');
215shouldBe('"Testing\\n\u{100f0} 1 2 3".match(/^[\u{1000}-\u{100ff}] 1 2 3/um)[0]', '"\u{100f0} 1 2 3"');
216shouldBe('"g\\n\u{1234} 1 2 3".match(/g\\n^[\u{1000}-\u{100ff}] 1 2 3/um)[0]', '"g\\n\u{1234} 1 2 3"');
217shouldBe('"g\\n\u{100f0} 1 2 3".match(/g\\n^[\u{1000}-\u{100ff}] 1 2 3/um)[0]', '"g\\n\u{100f0} 1 2 3"');
218shouldBe('"Testing \u{1234}\\n1 2 3".match(/Testing [\u{1000}-\u{100ff}]$/um)[0]', '"Testing \u{1234}"');
219shouldBe('"Testing \u{100f0}\\n1 2 3".match(/Testing [\u{1000}-\u{100ff}]$/um)[0]', '"Testing \u{100f0}"');
220shouldBe('"Testing \u{1234}\\n1 2 3".match(/g [\u{1000}-\u{100ff}]$\\n1/um)[0]', '"g \u{1234}\\n1"');
221shouldBe('"Testing \u{100f0}\\n1 2 3".match(/g [\u{1000}-\u{100ff}]$\\n1/um)[0]', '"g \u{100f0}\\n1"');
222
msaboff@apple.com5bc39062016-03-04 01:24:28 +0000223// Check that control letter escapes work with unicode flag
224shouldBe('"this is b\ba test".match(/is b\\cha test/u)[0].length', '11');
225
226// Check that invalid unicode patterns throw exceptions
227shouldBe('new RegExp("\\\\/", "u").source', '"\\\\/"');
228shouldThrow('r = new RegExp("\\\\u{110000}", "u")', '"SyntaxError: Invalid regular expression: invalid unicode {} escape"');
msaboff@apple.com6e73b412017-08-22 22:43:08 +0000229shouldThrow('r = new RegExp("\u{10405}{2147483648}", "u")', '"SyntaxError: Invalid regular expression: pattern exceeds string length limits"');
msaboff@apple.com5bc39062016-03-04 01:24:28 +0000230
231var invalidEscapeException = "SyntaxError: Invalid regular expression: invalid escaped character for unicode pattern";
232var newRegExp;
233
oliver@apple.com3909b162016-06-06 17:31:28 +0000234function shouldThrowInvalidEscape(pattern, error='invalidEscapeException')
msaboff@apple.com5bc39062016-03-04 01:24:28 +0000235{
236 newRegExp = 'r = new RegExp("' + pattern + '", "u")';
237
oliver@apple.com3909b162016-06-06 17:31:28 +0000238 shouldThrow(newRegExp, error);
msaboff@apple.com5bc39062016-03-04 01:24:28 +0000239}
240
241shouldThrowInvalidEscape("\\\\-");
242shouldThrowInvalidEscape("\\\\a");
243shouldThrowInvalidEscape("[\\\\a]");
244shouldThrowInvalidEscape("[\\\\b]");
245shouldThrowInvalidEscape("[\\\\B]");
246shouldThrowInvalidEscape("\\\\x");
247shouldThrowInvalidEscape("[\\\\x]");
248shouldThrowInvalidEscape("\\\\u");
249shouldThrowInvalidEscape("[\\\\u]");
250
oliver@apple.com3909b162016-06-06 17:31:28 +0000251shouldThrowInvalidEscape("\\\\u{", '"SyntaxError: Invalid regular expression: invalid unicode {} escape"');
252shouldThrowInvalidEscape("\\\\u{\\udead", '"SyntaxError: Invalid regular expression: invalid unicode {} escape"');
commit-queue@webkit.orgc9a6fe82017-04-13 02:51:18 +0000253
254// Check that invalid backreferences in unicode patterns throw exceptions.
255shouldThrow(`/\\1/u`);
256shouldThrow(`/\\2/u`);
257shouldThrow(`/\\3/u`);
258shouldThrow(`/\\4/u`);
259shouldThrow(`/\\5/u`);
260shouldThrow(`/\\6/u`);
261shouldThrow(`/\\7/u`);
262shouldThrow(`/\\8/u`);
263shouldThrow(`/\\9/u`);
264shouldNotThrow(`/(.)\\1/u`);
265shouldNotThrow(`/(.)(.)\\2/u`);
266shouldThrow(`/(.)(.)\\3/u`);
267
268// Invalid backreferences are okay in non-unicode patterns.
269shouldNotThrow(`/\\1/`);
270shouldNotThrow(`/\\2/`);
271shouldNotThrow(`/\\3/`);
272shouldNotThrow(`/\\4/`);
273shouldNotThrow(`/\\5/`);
274shouldNotThrow(`/\\6/`);
275shouldNotThrow(`/\\7/`);
276shouldNotThrow(`/\\8/`);
277shouldNotThrow(`/\\9/`);