2 "This page tests handling of characters which, according to ECMA 262, are not regular expression PatternCharacters. Those characters are: ^ $ \ . * + ? ( ) [ ] { } |"
5 // We test two cases, to match the two cases in the WREC parser.
6 // Test 1: the character stand-alone.
7 // Test 2: the character following a PatternCharacter.
11 // ^: Always allowed, always an assertion.
14 debug("\nTesting regexp: " + regexp);
15 shouldBeTrue("regexp.test('')");
16 shouldBe("regexp.lastIndex", "0");
19 debug("\nTesting regexp: " + regexp);
20 shouldBeTrue("regexp.test('\\n\\n')");
21 shouldBe("regexp.lastIndex", "1");
23 // $: Always allowed, always an assertion.
26 debug("\nTesting regexp: " + regexp);
27 shouldBeTrue("regexp.test('')");
28 shouldBe("regexp.lastIndex", "0");
31 debug("\nTesting regexp: " + regexp);
32 shouldBeTrue("regexp.test('\\n\\n')");
33 shouldBe("regexp.lastIndex", "1");
35 // \: Only allowed as a prefix. Contrary to spec, always treated as an
36 // IdentityEscape when followed by an invalid escape postfix.
38 debug("\nTesting regexp: " + regexp);
39 shouldBeTrue("regexp.test('z')"); // invalid postfix => IdentityEscape
42 debug("\nTesting regexp: " + regexp);
43 shouldBeTrue("regexp.test('az')"); // invalid postfix => IdentityEscape
46 debug("\nTesting regexp: " + regexp);
47 shouldBeTrue("regexp.test('_')"); // invalid postfix => IdentityEscape
50 debug("\nTesting regexp: " + regexp);
51 shouldBeTrue("regexp.test('a_')"); // invalid postfix => IdentityEscape
53 debug("\nTesting regexp: " + "[invalid \\ variations]");
54 shouldThrow("/\\/"); // no postfix => not allowed
55 shouldThrow("/a\\/"); // no postfix => not allowed
57 // .: Always allowed, always a non-newline wildcard.
59 debug("\nTesting regexp: " + regexp);
60 shouldBeTrue("regexp.test('a')");
61 shouldBeFalse("regexp.test('\\n')");
64 debug("\nTesting regexp: " + regexp);
65 shouldBeTrue("regexp.test('aa')");
66 shouldBeFalse("regexp.test('a\\n')");
68 // *: Only allowed as a postfix to a PatternCharacter. Behaves as a {0,Infinity} quantifier.
70 debug("\nTesting regexp: " + regexp);
71 shouldBeTrue("regexp.test('b')");
72 shouldBe("regexp.lastIndex", "0");
74 shouldBeTrue("regexp.test('aaba')");
75 shouldBe("regexp.lastIndex", "2");
77 debug("\nTesting regexp: " + "[invalid * variations]");
78 shouldThrow("/*/"); // Unterminated comment.
79 shouldThrow("/^*/"); // Prefixed by ^ to avoid confusion with comment syntax.
81 // +: Only allowed as a postfix to a PatternCharacter. Behaves as a {1,Infinity} quantifier.
83 debug("\nTesting regexp: " + regexp);
84 shouldBeFalse("regexp.test('b')");
86 shouldBeTrue("regexp.test('aaba')");
87 shouldBe("regexp.lastIndex", "2");
89 debug("\nTesting regexp: " + "[invalid + variations]");
92 // ?: Only allowed as a postfix to a PatternCharacter. Behaves as a {0,1} quantifier.
94 debug("\nTesting regexp: " + regexp);
95 shouldBeTrue("regexp.test('b')");
96 shouldBe("regexp.lastIndex", "0");
98 shouldBeTrue("regexp.test('aaba')");
99 shouldBe("regexp.lastIndex", "1");
101 debug("\nTesting regexp: " + "[invalid ? variations]");
104 // (: Only allowed if it matches a ).
105 debug("\nTesting regexp: " + "[invalid ( variations]");
109 // ): Only allowed if it matches a (.
110 debug("\nTesting regexp: " + "[invalid ) variations]");
114 // [: Only allowed if it matches a ] and the stuff in between is a valid character class.
115 debug("\nTesting regexp: " + "[invalid [ variations]");
119 shouldThrow("/[b-a]/");
120 shouldThrow("/a[b-a]/");
122 // ]: Closes a ]. Contrary to spec, if no [ was seen, acts as a regular PatternCharacter.
124 debug("\nTesting regexp: " + regexp);
125 shouldBeTrue("regexp.test(']')");
126 shouldBe("regexp.lastIndex", "1");
129 debug("\nTesting regexp: " + regexp);
130 shouldBeTrue("regexp.test('a]')");
131 shouldBe("regexp.lastIndex", "2");
133 // {: Begins a quantifier. Contrary to spec, if no } is seen, or if the stuff in
134 // between does not lex as a quantifier, acts as a regular PatternCharacter. If
135 // the stuff in between does lex as a quantifier, but the quantifier is invalid,
136 // acts as a syntax error.
138 debug("\nTesting regexp: " + regexp);
139 shouldBeTrue("regexp.test('{')");
140 shouldBe("regexp.lastIndex", "1");
143 debug("\nTesting regexp: " + regexp);
144 shouldBeTrue("regexp.test('a{')");
145 shouldBe("regexp.lastIndex", "2");
148 debug("\nTesting regexp: " + regexp);
149 shouldBeTrue("regexp.test('{a')");
150 shouldBe("regexp.lastIndex", "2");
153 debug("\nTesting regexp: " + regexp);
154 shouldBeTrue("regexp.test('a{a')");
155 shouldBe("regexp.lastIndex", "3");
158 debug("\nTesting regexp: " + regexp);
159 shouldBeTrue("regexp.test('{1,')");
160 shouldBe("regexp.lastIndex", "3");
163 debug("\nTesting regexp: " + regexp);
164 shouldBeTrue("regexp.test('a{1,')");
165 shouldBe("regexp.lastIndex", "4");
168 debug("\nTesting regexp: " + regexp);
169 shouldBeTrue("regexp.test('{1,a')");
170 shouldBe("regexp.lastIndex", "4");
173 debug("\nTesting regexp: " + regexp);
174 shouldBeTrue("regexp.test('{1,0')");
175 shouldBe("regexp.lastIndex", "4");
178 debug("\nTesting regexp: " + regexp);
179 shouldBeTrue("regexp.test('{1, 0}')");
180 shouldBe("regexp.lastIndex", "6");
182 regexp = /a{1, 0}/gm;
183 debug("\nTesting regexp: " + regexp);
184 shouldBeTrue("regexp.test('a{1, 0}')");
185 shouldBe("regexp.lastIndex", "7");
187 try { regexp = new RegExp("a{1,0", "gm"); } catch(e) { regexp = e; }; // Work around exception thrown in Firefox -- probably too weird to be worth matching.
188 debug("\nTesting regexp: " + regexp);
189 shouldBeTrue("regexp.test('a{1,0')");
190 shouldBe("regexp.lastIndex", "5");
193 debug("\nTesting regexp: " + regexp);
194 shouldBeTrue("regexp.test('a')");
195 shouldBe("regexp.lastIndex", "0");
196 shouldBeTrue("regexp.test('b')");
197 shouldBe("regexp.lastIndex", "0");
199 debug("\nTesting regexp: " + "[invalid {} variations]");
200 shouldThrow("/{0}/");
201 shouldThrow("/{1,0}/");
202 shouldThrow("/a{1,0}/");
204 // }: Ends a quantifier. Same rules as for {.
206 debug("\nTesting regexp: " + regexp);
207 shouldBeTrue("regexp.test('}')");
208 shouldBe("regexp.lastIndex", "1");
211 debug("\nTesting regexp: " + regexp);
212 shouldBeTrue("regexp.test('a}')");
213 shouldBe("regexp.lastIndex", "2");
215 // |: Always allowed, always separates two alternatives.
216 regexp = new RegExp("", "gm");
217 debug("\nTesting regexp: " + regexp);
218 shouldBeTrue("regexp.test('')");
219 shouldBe("regexp.lastIndex", "0");
222 debug("\nTesting regexp: " + regexp);
223 shouldBeTrue("regexp.test('|')");
224 shouldBe("regexp.lastIndex", "0");
227 debug("\nTesting regexp: " + regexp);
228 shouldBeTrue("regexp.test('|')");
229 shouldBe("regexp.lastIndex", "0");