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
22 m = "abcabc".match(/ca/);
23 ok(typeof(m) === "object", "typeof m is not object");
24 ok(m.length === 1, "m.length is not 1");
25 ok(m["0"] === "ca", "m[0] is not \"ca\"");
27 m = "abcabc".match(/ab/);
28 ok(typeof(m) === "object", "typeof m is not object");
29 ok(m.length === 1, "m.length is not 1");
30 ok(m["0"] === "ab", "m[0] is not \"ab\"");
32 m = "abcabc".match(/ab/g);
33 ok(typeof(m) === "object", "typeof m is not object");
34 ok(m.length === 2, "m.length is not 1");
35 ok(m["0"] === "ab", "m[0] is not \"ab\"");
36 ok(m["1"] === "ab", "m[1] is not \"ab\"");
38 m = "abcabc".match(/Ab/g);
39 ok(typeof(m) === "object", "typeof m is not object");
40 ok(m === null, "m is not null");
42 m = "abcabc".match(/Ab/gi);
43 ok(typeof(m) === "object", "typeof m is not object");
44 ok(m.length === 2, "m.length is not 1");
45 ok(m["0"] === "ab", "m[0] is not \"ab\"");
46 ok(m["1"] === "ab", "m[1] is not \"ab\"");
48 m = "aaabcabc".match(/a+b/g);
49 ok(typeof(m) === "object", "typeof m is not object");
50 ok(m.length === 2, "m.length is not 2");
51 ok(m["0"] === "aaab", "m[0] is not \"ab\"");
52 ok(m["1"] === "ab", "m[1] is not \"ab\"");
54 m = "aaa\\\\cabc".match(/\\/g);
55 ok(typeof(m) === "object", "typeof m is not object");
56 ok(m.length === 2, "m.length is not 2");
57 ok(m["0"] === "\\", "m[0] is not \"\\\"");
58 ok(m["1"] === "\\", "m[1] is not \"\\\"");
60 m = "abcabc".match(new RegExp("ab"));
61 ok(typeof(m) === "object", "typeof m is not object");
62 ok(m.length === 1, "m.length is not 1");
63 ok(m["0"] === "ab", "m[0] is not \"ab\"");
65 m = "abcabc".match(new RegExp("ab","g"));
66 ok(typeof(m) === "object", "typeof m is not object");
67 ok(m.length === 2, "m.length is not 1");
68 ok(m["0"] === "ab", "m[0] is not \"ab\"");
69 ok(m["1"] === "ab", "m[1] is not \"ab\"");
71 m = "abcabc".match(new RegExp(/ab/g));
72 ok(typeof(m) === "object", "typeof m is not object");
73 ok(m.length === 2, "m.length is not 1");
74 ok(m["0"] === "ab", "m[0] is not \"ab\"");
75 ok(m["1"] === "ab", "m[1] is not \"ab\"");
77 m = "abcabc".match(new RegExp("ab","g", "test"));
78 ok(typeof(m) === "object", "typeof m is not object");
79 ok(m.length === 2, "m.length is not 1");
80 ok(m["0"] === "ab", "m[0] is not \"ab\"");
81 ok(m["1"] === "ab", "m[1] is not \"ab\"");
83 r = "- [test] -".replace(/\[([^\[]+)\]/g, "success");
84 ok(r === "- success -", "r = " + r + " expected '- success -'");
86 r = "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
87 ok(r === "aa aa", "r = " + r + "aa aa");
89 r = "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
90 ok(r === "aa [test]", "r = " + r + " expected 'aa [test]'");
92 r = "- [test] -".replace(/\[([^\[]+)\]/g);
93 ok(r === "- undefined -", "r = " + r + " expected '- undefined -'");
95 r = "- [test] -".replace(/\[([^\[]+)\]/g, true);
96 ok(r === "- true -", "r = " + r + " expected '- true -'");
98 r = "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
99 ok(r === "- true -", "r = " + r + " expected '- true -'");
103 function replaceFunc1(m, off, str) {
104 ok(arguments.length === 3, "arguments.length = " + arguments.length);
108 ok(m === "[test1]", "m = " + m + " expected [test1]");
109 ok(off === 0, "off = " + off + " expected 0");
112 ok(m === "[test2]", "m = " + m + " expected [test2]");
113 ok(off === 8, "off = " + off + " expected 8");
116 ok(false, "unexpected call");
119 ok(str === "[test1] [test2]", "str = " + arguments[3]);
123 r = "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1);
124 ok(r === "r0 r1", "r = " + r + " expected 'r0 r1'");
128 function replaceFunc2(m, subm, off, str) {
129 ok(arguments.length === 4, "arguments.length = " + arguments.length);
133 ok(subm === "test1", "subm = " + subm);
134 ok(m === "[test1]", "m = " + m + " expected [test1]");
135 ok(off === 0, "off = " + off + " expected 0");
138 ok(subm === "test2", "subm = " + subm);
139 ok(m === "[test2]", "m = " + m + " expected [test2]");
140 ok(off === 8, "off = " + off + " expected 8");
143 ok(false, "unexpected call");
146 ok(str === "[test1] [test2]", "str = " + arguments[3]);
150 r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
151 ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
153 r = "1,,2,3".split(/,+/g);
154 ok(r.length === 3, "r.length = " + r.length);
155 ok(r[0] === "1", "r[0] = " + r[0]);
156 ok(r[1] === "2", "r[1] = " + r[1]);
157 ok(r[2] === "3", "r[2] = " + r[2]);
159 r = "1,,2,3".split(/,+/);
160 ok(r.length === 3, "r.length = " + r.length);
161 ok(r[0] === "1", "r[0] = " + r[0]);
162 ok(r[1] === "2", "r[1] = " + r[1]);
163 ok(r[2] === "3", "r[2] = " + r[2]);
165 r = "1,,2,".split(/,+/);
166 ok(r.length === 2, "r.length = " + r.length);
167 ok(r[0] === "1", "r[0] = " + r[0]);
168 ok(r[1] === "2", "r[1] = " + r[1]);