2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
25 m = re.exec(" aabaaa");
26 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
27 ok(m.index === 1, "m.index = " + m.index);
28 ok(m.input === " aabaaa", "m.input = " + m.input);
29 ok(m.length === 1, "m.length = " + m.length);
30 ok(m[0] === "aa", "m[0] = " + m[0]);
32 m = re.exec(" aabaaa");
33 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
34 ok(m.index === 1, "m.index = " + m.index);
35 ok(m.input === " aabaaa", "m.input = " + m.input);
36 ok(m.length === 1, "m.length = " + m.length);
37 ok(m[0] === "aa", "m[0] = " + m[0]);
40 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
42 m = re.exec(" aabaaa");
43 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
44 ok(m.index === 1, "m.index = " + m.index);
45 ok(m.input === " aabaaa", "m.input = " + m.input);
46 ok(m.length === 1, "m.length = " + m.length);
47 ok(m[0] === "aa", "m[0] = " + m[0]);
49 m = re.exec(" aabaaa");
50 ok(re.lastIndex === 7, "re.lastIndex = " + re.lastIndex);
51 ok(m.index === 4, "m.index = " + m.index);
52 ok(m.input === " aabaaa", "m.input = " + m.input);
53 ok(m.length === 1, "m.length = " + m.length);
54 ok(m[0] === "aaa", "m[0] = " + m[0]);
56 m = re.exec(" aabaaa");
57 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
58 ok(m === null, "m is not null");
61 ok(re.lastIndex === 16, "re.lastIndex = " + re.lastIndex);
64 ok(m === null, "m is not null");
65 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
68 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex);
71 ok(m === null, "m is not null");
72 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
74 m = /(a|b)+|(c)/.exec("aa");
75 ok(m[0] === "aa", "m[0] = " + m[0]);
76 ok(m[1] === "a", "m[1] = " + m[1]);
77 ok(m[2] === "", "m[2] = " + m[2]);
80 ok(b === true, "re.test(' a ') returned " + b);
81 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
84 ok(b === false, "re.test(' a ') returned " + b);
85 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
88 m = re.exec(" [test] ");
89 ok(re.lastIndex === 7, "re.lastIndex = " + re.lastIndex);
90 ok(m.index === 1, "m.index = " + m.index);
91 ok(m.input === " [test] ", "m.input = " + m.input);
92 ok(m.length === 2, "m.length = " + m.length);
93 ok(m[0] === "[test]", "m[0] = " + m[0]);
94 ok(m[1] === "test", "m[1] = " + m[1]);
97 ok(b === true, "/a*/.test() returned " + b);
99 m = "abcabc".match(/ca/);
100 ok(typeof(m) === "object", "typeof m is not object");
101 ok(m.length === 1, "m.length is not 1");
102 ok(m["0"] === "ca", "m[0] is not \"ca\"");
104 m = "abcabc".match(/ab/);
105 ok(typeof(m) === "object", "typeof m is not object");
106 ok(m.length === 1, "m.length is not 1");
107 ok(m["0"] === "ab", "m[0] is not \"ab\"");
109 m = "abcabc".match(/ab/g);
110 ok(typeof(m) === "object", "typeof m is not object");
111 ok(m.length === 2, "m.length is not 2");
112 ok(m["0"] === "ab", "m[0] is not \"ab\"");
113 ok(m["1"] === "ab", "m[1] is not \"ab\"");
115 m = "abcabc".match(/Ab/g);
116 ok(typeof(m) === "object", "typeof m is not object");
117 ok(m === null, "m is not null");
119 m = "abcabc".match(/Ab/gi);
120 ok(typeof(m) === "object", "typeof m is not object");
121 ok(m.length === 2, "m.length is not 2");
122 ok(m["0"] === "ab", "m[0] is not \"ab\"");
123 ok(m["1"] === "ab", "m[1] is not \"ab\"");
125 m = "aaabcabc".match(/a+b/g);
126 ok(typeof(m) === "object", "typeof m is not object");
127 ok(m.length === 2, "m.length is not 2");
128 ok(m["0"] === "aaab", "m[0] is not \"ab\"");
129 ok(m["1"] === "ab", "m[1] is not \"ab\"");
131 m = "aaa\\\\cabc".match(/\\/g);
132 ok(typeof(m) === "object", "typeof m is not object");
133 ok(m.length === 2, "m.length is not 2");
134 ok(m["0"] === "\\", "m[0] is not \"\\\"");
135 ok(m["1"] === "\\", "m[1] is not \"\\\"");
137 m = "abcabc".match(new RegExp("ab"));
138 ok(typeof(m) === "object", "typeof m is not object");
139 ok(m.length === 1, "m.length is not 1");
140 ok(m["0"] === "ab", "m[0] is not \"ab\"");
142 m = "abcabc".match(new RegExp("ab","g"));
143 ok(typeof(m) === "object", "typeof m is not object");
144 ok(m.length === 2, "m.length is not 2");
145 ok(m["0"] === "ab", "m[0] is not \"ab\"");
146 ok(m["1"] === "ab", "m[1] is not \"ab\"");
148 m = "abcabc".match(new RegExp(/ab/g));
149 ok(typeof(m) === "object", "typeof m is not object");
150 ok(m.length === 2, "m.length is not 2");
151 ok(m["0"] === "ab", "m[0] is not \"ab\"");
152 ok(m["1"] === "ab", "m[1] is not \"ab\"");
154 m = "abcabc".match(new RegExp("ab","g", "test"));
155 ok(typeof(m) === "object", "typeof m is not object");
156 ok(m.length === 2, "m.length is not 2");
157 ok(m["0"] === "ab", "m[0] is not \"ab\"");
158 ok(m["1"] === "ab", "m[1] is not \"ab\"");
160 m = "abcabcg".match("ab", "g");
161 ok(typeof(m) === "object", "typeof m is not object");
162 ok(m.length === 1, "m.length is not 1");
163 ok(m["0"] === "ab", "m[0] is not \"ab\"");
165 m = "abcabc".match();
166 ok(m === null, "m is not null");
168 r = "- [test] -".replace(/\[([^\[]+)\]/g, "success");
169 ok(r === "- success -", "r = " + r + " expected '- success -'");
171 r = "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
172 ok(r === "aa aa", "r = " + r + "aa aa");
174 r = "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
175 ok(r === "aa [test]", "r = " + r + " expected 'aa [test]'");
177 r = "- [test] -".replace(/\[([^\[]+)\]/g);
178 ok(r === "- undefined -", "r = " + r + " expected '- undefined -'");
180 r = "- [test] -".replace(/\[([^\[]+)\]/g, true);
181 ok(r === "- true -", "r = " + r + " expected '- true -'");
183 r = "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
184 ok(r === "- true -", "r = " + r + " expected '- true -'");
188 function replaceFunc1(m, off, str) {
189 ok(arguments.length === 3, "arguments.length = " + arguments.length);
193 ok(m === "[test1]", "m = " + m + " expected [test1]");
194 ok(off === 0, "off = " + off + " expected 0");
197 ok(m === "[test2]", "m = " + m + " expected [test2]");
198 ok(off === 8, "off = " + off + " expected 8");
201 ok(false, "unexpected call");
204 ok(str === "[test1] [test2]", "str = " + arguments[3]);
208 r = "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1);
209 ok(r === "r0 r1", "r = " + r + " expected 'r0 r1'");
213 function replaceFunc2(m, subm, off, str) {
214 ok(arguments.length === 4, "arguments.length = " + arguments.length);
218 ok(subm === "test1", "subm = " + subm);
219 ok(m === "[test1]", "m = " + m + " expected [test1]");
220 ok(off === 0, "off = " + off + " expected 0");
223 ok(subm === "test2", "subm = " + subm);
224 ok(m === "[test2]", "m = " + m + " expected [test2]");
225 ok(off === 8, "off = " + off + " expected 8");
228 ok(false, "unexpected call");
231 ok(str === "[test1] [test2]", "str = " + arguments[3]);
235 r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
236 ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
238 r = "$1,$2".replace(/(\$(\d))/g, "$$1-$1$2");
239 ok(r === "$1-$11,$1-$22", "r = '" + r + "' expected '$1-$11,$1-$22'");
241 r = "abc &1 123".replace(/(\&(\d))/g, "$&");
242 ok(r === "abc &1 123", "r = '" + r + "' expected 'abc &1 123'");
244 r = "abc &1 123".replace(/(\&(\d))/g, "$'");
245 ok(r === "abc 123 123", "r = '" + r + "' expected 'abc 123 123'");
247 r = "abc &1 123".replace(/(\&(\d))/g, "$`");
248 ok(r === "abc abc 123", "r = '" + r + "' expected 'abc abc 123'");
250 r = "abc &1 123".replace(/(\&(\d))/g, "$3");
251 ok(r === "abc $3 123", "r = '" + r + "' expected 'abc $3 123'");
253 r = "abc &1 123".replace(/(\&(\d))/g, "$");
254 ok(r === "abc $ 123", "r = '" + r + "' expected 'abc $ 123'");
256 r = "abc &1 123".replace(/(\&(\d))/g, "$a");
257 ok(r === "abc $a 123", "r = '" + r + "' expected 'abc $a 123'");
259 r = "abc &1 123".replace(/(\&(\d))/g, "$11");
260 ok(r === "abc &11 123", "r = '" + r + "' expected 'abc &11 123'");
262 r = "abc &1 123".replace(/(\&(\d))/g, "$0");
263 ok(r === "abc $0 123", "r = '" + r + "' expected 'abc $0 123'");
265 r = "1 2 3".replace("2", "$&");
266 ok(r === "1 $& 3", "r = '" + r + "' expected '1 $& 3'");
268 r = "1 2 3".replace("2", "$'");
269 ok(r === "1 $' 3", "r = '" + r + "' expected '1 $' 3'");
271 r = "1,,2,3".split(/,+/g);
272 ok(r.length === 3, "r.length = " + r.length);
273 ok(r[0] === "1", "r[0] = " + r[0]);
274 ok(r[1] === "2", "r[1] = " + r[1]);
275 ok(r[2] === "3", "r[2] = " + r[2]);
277 r = "1,,2,3".split(/,+/);
278 ok(r.length === 3, "r.length = " + r.length);
279 ok(r[0] === "1", "r[0] = " + r[0]);
280 ok(r[1] === "2", "r[1] = " + r[1]);
281 ok(r[2] === "3", "r[2] = " + r[2]);
283 r = "1,,2,".split(/,+/);
284 ok(r.length === 2, "r.length = " + r.length);
285 ok(r[0] === "1", "r[0] = " + r[0]);
286 ok(r[1] === "2", "r[1] = " + r[1]);
289 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
292 ok(re.source === "a\\bc[^d]", "re.source = '" + re.source + "', expected 'a\\bc[^d]'");
295 ok(re === RegExp(re), "re !== RegExp(re)");
297 re = RegExp("abc[^d]", "g");
298 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
301 ok(re === RegExp(re, undefined), "re !== RegExp(re, undefined)");
304 ok(re === RegExp(re, undefined, 1), "re !== RegExp(re, undefined, 1)");
307 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
310 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
311 ok(m.index === 1, "m.index = " + m.index + " expected 1");
314 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
315 ok(m === null, "m = " + m + " expected null");
318 m = re.exec(" a a ");
319 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
320 ok(m.index === 3, "m.index = " + m.index + " expected 3");
323 ok(re.lastIndex === "2", "re.lastIndex = " + re.lastIndex + " expected '2'");
324 m = re.exec(" a a ");
325 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
326 ok(m.index === 3, "m.index = " + m.index + " expected 3");
329 var obj = new Object();
330 obj.valueOf = function() { return li; };
333 ok(re.lastIndex === obj, "re.lastIndex = " + re.lastIndex + " expected obj");
335 m = re.exec(" a a ");
336 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
337 ok(m.index === 1, "m.index = " + m.index + " expected 1");
340 re.lastIndex = "test";
341 ok(re.lastIndex === "test", "re.lastIndex = " + re.lastIndex + " expected 'test'");
342 m = re.exec(" a a ");
343 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
344 ok(m.index === 1, "m.index = " + m.index + " expected 1");
348 ok(re.lastIndex === 3.9, "re.lastIndex = " + re.lastIndex + " expected 3.9");
349 m = re.exec(" a a ");
350 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
351 ok(m.index === 3, "m.index = " + m.index + " expected 3");
353 obj.valueOf = function() { throw 0; }
355 ok(re.lastIndex === obj, "unexpected re.lastIndex");
356 m = re.exec(" a a ");
357 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
358 ok(m.index === 1, "m.index = " + m.index + " expected 1");
361 ok(re.lastIndex === -3, "re.lastIndex = " + re.lastIndex + " expected -3");
362 m = re.exec(" a a ");
363 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
364 ok(m === null, "m = " + m + " expected null");
367 ok(re.lastIndex === -1, "re.lastIndex = " + re.lastIndex + " expected -1");
369 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
370 ok(m === null, "m = " + m + " expected null");