1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Tests captures in positive and negative look-ahead in regular expressions.
30 function stringEscape(string) {
31 // Converts string to source literal.
32 return '"' + string.replace(/["\\]/g, "\\$1") + '"';
35 function testRE(re, input, expected_result) {
36 var testName = re + ".test(" + stringEscape(input) +")";
37 if (expected_result) {
38 assertTrue(re.test(input), testName);
40 assertFalse(re.test(input), testName);
44 function execRE(re, input, expected_result) {
45 var testName = re + ".exec('" + stringEscape(input) +"')";
46 assertEquals(expected_result, re.exec(input), testName);
49 // Test of simple positive lookahead.
52 testRE(re, "a", true);
53 testRE(re, "b", false);
54 execRE(re, "a", [""]);
57 testRE(re, "foo", true);
58 testRE(re, "boo", false);
59 testRE(re, "fao", false);
60 testRE(re, "foa", false);
61 execRE(re, "foo", ["fo"]);
64 testRE(re, ".a! ", true);
65 testRE(re, ".! ", false);
66 testRE(re, ".ab! ", true);
67 execRE(re, ".ab! ", ["b"]);
69 re = /(?=f(?=[^f]o))../;
70 testRE(re, ", foo!", true);
71 testRE(re, ", fo!", false);
72 testRE(re, ", ffo", false);
73 execRE(re, ", foo!", ["fo"]);
75 // Positive lookahead with captures.
76 re = /^[^\'\"]*(?=([\'\"])).*\1(\w+)\1/;
77 testRE(re, " 'foo' ", true);
78 testRE(re, ' "foo" ', true);
79 testRE(re, " \" 'foo' ", false);
80 testRE(re, " ' \"foo\" ", false);
81 testRE(re, " 'foo\" ", false);
82 testRE(re, " \"foo' ", false);
83 execRE(re, " 'foo' ", [" 'foo'", "'", "foo"]);
84 execRE(re, ' "foo" ', [' "foo"', '"', 'foo']);
86 // Captures are cleared on backtrack past the look-ahead.
87 re = /^(?:(?=(.))a|b)\1$/;
88 testRE(re, "aa", true);
89 testRE(re, "b", true);
90 testRE(re, "bb", false);
91 testRE(re, "a", false);
92 execRE(re, "aa", ["aa", "a"]);
93 execRE(re, "b", ["b", undefined]);
95 re = /^(?=(.)(?=(.)\1\2)\2\1)\1\2/;
96 testRE(re, "abab", true);
97 testRE(re, "ababxxxxxxxx", true);
98 testRE(re, "aba", false);
99 execRE(re, "abab", ["ab", "a", "b"]);
101 re = /^(?:(?=(.))a|b|c)$/;
102 testRE(re, "a", true);
103 testRE(re, "b", true);
104 testRE(re, "c", true);
105 testRE(re, "d", false);
106 execRE(re, "a", ["a", "a"]);
107 execRE(re, "b", ["b", undefined]);
108 execRE(re, "c", ["c", undefined]);
110 execRE(/^(?=(b))b/, "b", ["b", "b"]);
111 execRE(/^(?:(?=(b))|a)b/, "ab", ["ab", undefined]);
112 execRE(/^(?:(?=(b)(?:(?=(c))|d))|)bd/, "bd", ["bd", "b", undefined]);
116 // Test of Negative Look-Ahead.
119 testRE(re, "y", true);
120 testRE(re, "x", false);
121 execRE(re, "y", ["y"]);
124 testRE(re, "4", true);
125 execRE(re, "4", ["4", undefined]);
126 execRE(re, "x", ["", undefined]);
129 // Test mixed nested look-ahead with captures.
131 re = /^(?=(x)(?=(y)))/;
132 testRE(re, "xy", true);
133 testRE(re, "xz", false);
134 execRE(re, "xy", ["", "x", "y"]);
136 re = /^(?!(x)(?!(y)))/;
137 testRE(re, "xy", true);
138 testRE(re, "xz", false);
139 execRE(re, "xy", ["", undefined, undefined]);
141 re = /^(?=(x)(?!(y)))/;
142 testRE(re, "xz", true);
143 testRE(re, "xy", false)
144 execRE(re, "xz", ["", "x", undefined]);
146 re = /^(?!(x)(?=(y)))/;
147 testRE(re, "xz", true);
148 testRE(re, "xy", false);
149 execRE(re, "xz", ["", undefined, undefined]);
151 re = /^(?=(x)(?!(y)(?=(z))))/;
152 testRE(re, "xaz", true);
153 testRE(re, "xya", true);
154 testRE(re, "xyz", false);
155 testRE(re, "a", false);
156 execRE(re, "xaz", ["", "x", undefined, undefined]);
157 execRE(re, "xya", ["", "x", undefined, undefined]);
159 re = /^(?!(x)(?=(y)(?!(z))))/;
160 testRE(re, "a", true);
161 testRE(re, "xa", true);
162 testRE(re, "xyz", true);
163 testRE(re, "xya", false);
164 execRE(re, "a", ["", undefined, undefined, undefined]);
165 execRE(re, "xa", ["", undefined, undefined, undefined]);
166 execRE(re, "xyz", ["", undefined, undefined, undefined]);