1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description("This page tests handling of parentheses subexpressions.");
25
26var regexp1 = /.*blah.*/;
27shouldBeNull("regexp1.exec('test')");
28shouldBe("regexp1.exec('blah')", "['blah']");
29shouldBe("regexp1.exec('1blah')", "['1blah']");
30shouldBe("regexp1.exec('blah1')", "['blah1']");
31shouldBe("regexp1.exec('blah blah blah')", "['blah blah blah']");
32shouldBe("regexp1.exec('blah\\nsecond')", "['blah']");
33shouldBe("regexp1.exec('first\\nblah')", "['blah']");
34shouldBe("regexp1.exec('first\\nblah\\nthird')", "['blah']");
35shouldBe("regexp1.exec('first\\nblah2\\nblah3')", "['blah2']");
36
37var regexp2 = /^.*blah.*/;
38shouldBeNull("regexp2.exec('test')");
39shouldBe("regexp2.exec('blah')", "['blah']");
40shouldBe("regexp2.exec('1blah')", "['1blah']");
41shouldBe("regexp2.exec('blah1')", "['blah1']");
42shouldBe("regexp2.exec('blah blah blah')", "['blah blah blah']");
43shouldBe("regexp2.exec('blah\\nsecond')", "['blah']");
44shouldBeNull("regexp2.exec('first\\nblah')");
45shouldBeNull("regexp2.exec('first\\nblah\\nthird')");
46shouldBeNull("regexp2.exec('first\\nblah2\\nblah3')");
47
48var regexp3 = /.*blah.*$/;
49shouldBeNull("regexp3.exec('test')");
50shouldBe("regexp3.exec('blah')", "['blah']");
51shouldBe("regexp3.exec('1blah')", "['1blah']");
52shouldBe("regexp3.exec('blah1')", "['blah1']");
53shouldBe("regexp3.exec('blah blah blah')", "['blah blah blah']");
54shouldBeNull("regexp3.exec('blah\\nsecond')");
55shouldBe("regexp3.exec('first\\nblah')", "['blah']");
56shouldBeNull("regexp3.exec('first\\nblah\\nthird')");
57shouldBe("regexp3.exec('first\\nblah2\\nblah3')", "['blah3']");
58
59var regexp4 = /^.*blah.*$/;
60shouldBeNull("regexp4.exec('test')");
61shouldBe("regexp4.exec('blah')", "['blah']");
62shouldBe("regexp4.exec('1blah')", "['1blah']");
63shouldBe("regexp4.exec('blah1')", "['blah1']");
64shouldBe("regexp4.exec('blah blah blah')", "['blah blah blah']");
65shouldBeNull("regexp4.exec('blah\\nsecond')");
66shouldBeNull("regexp4.exec('first\\nblah')");
67shouldBeNull("regexp4.exec('first\\nblah\\nthird')");
68shouldBeNull("regexp4.exec('first\\nblah2\\nblah3')");
69
70var regexp5 = /.*?blah.*/;
71shouldBeNull("regexp5.exec('test')");
72shouldBe("regexp5.exec('blah')", "['blah']");
73shouldBe("regexp5.exec('1blah')", "['1blah']");
74shouldBe("regexp5.exec('blah1')", "['blah1']");
75shouldBe("regexp5.exec('blah blah blah')", "['blah blah blah']");
76shouldBe("regexp5.exec('blah\\nsecond')", "['blah']");
77shouldBe("regexp5.exec('first\\nblah')", "['blah']");
78shouldBe("regexp5.exec('first\\nblah\\nthird')", "['blah']");
79shouldBe("regexp5.exec('first\\nblah2\\nblah3')", "['blah2']");
80
81var regexp6 = /.*blah.*?/;
82shouldBeNull("regexp6.exec('test')");
83shouldBe("regexp6.exec('blah')", "['blah']");
84shouldBe("regexp6.exec('1blah')", "['1blah']");
85shouldBe("regexp6.exec('blah1')", "['blah']");
86shouldBe("regexp6.exec('blah blah blah')", "['blah blah blah']");
87shouldBe("regexp6.exec('blah\\nsecond')", "['blah']");
88shouldBe("regexp6.exec('first\\nblah')", "['blah']");
89shouldBe("regexp6.exec('first\\nblah\\nthird')", "['blah']");
90shouldBe("regexp6.exec('first\\nblah2\\nblah3')", "['blah']");
91
92var regexp7 = /^.*?blah.*?$/;
93shouldBeNull("regexp7.exec('test')");
94shouldBe("regexp7.exec('blah')", "['blah']");
95shouldBe("regexp7.exec('1blah')", "['1blah']");
96shouldBe("regexp7.exec('blah1')", "['blah1']");
97shouldBe("regexp7.exec('blah blah blah')", "['blah blah blah']");
98shouldBeNull("regexp7.exec('blah\\nsecond')");
99shouldBeNull("regexp7.exec('first\\nblah')");
100shouldBeNull("regexp7.exec('first\\nblah\\nthird')");
101shouldBeNull("regexp7.exec('first\\nblah2\\nblah3')");
102
103var regexp8 = /^(.*)blah.*$/;
104shouldBeNull("regexp8.exec('test')");
105shouldBe("regexp8.exec('blah')", "['blah','']");
106shouldBe("regexp8.exec('1blah')", "['1blah','1']");
107shouldBe("regexp8.exec('blah1')", "['blah1','']");
108shouldBe("regexp8.exec('blah blah blah')", "['blah blah blah','blah blah ']");
109shouldBeNull("regexp8.exec('blah\\nsecond')");
110shouldBeNull("regexp8.exec('first\\nblah')");
111shouldBeNull("regexp8.exec('first\\nblah\\nthird')");
112shouldBeNull("regexp8.exec('first\\nblah2\\nblah3')");
113
114var regexp9 = /.*blah.*/m;
115shouldBeNull("regexp9.exec('test')");
116shouldBe("regexp9.exec('blah')", "['blah']");
117shouldBe("regexp9.exec('1blah')", "['1blah']");
118shouldBe("regexp9.exec('blah1')", "['blah1']");
119shouldBe("regexp9.exec('blah blah blah')", "['blah blah blah']");
120shouldBe("regexp9.exec('blah\\nsecond')", "['blah']");
121shouldBe("regexp9.exec('first\\nblah')", "['blah']");
122shouldBe("regexp9.exec('first\\nblah\\nthird')", "['blah']");
123shouldBe("regexp9.exec('first\\nblah2\\nblah3')", "['blah2']");
124
125var regexp10 = /^.*blah.*/m;
126shouldBeNull("regexp10.exec('test')");
127shouldBe("regexp10.exec('blah')", "['blah']");
128shouldBe("regexp10.exec('1blah')", "['1blah']");
129shouldBe("regexp10.exec('blah1')", "['blah1']");
130shouldBe("regexp10.exec('blah blah blah')", "['blah blah blah']");
131shouldBe("regexp10.exec('blah\\nsecond')", "['blah']");
132shouldBe("regexp10.exec('first\\nblah')", "['blah']");
133shouldBe("regexp10.exec('first\\nblah\\nthird')", "['blah']");
134shouldBe("regexp10.exec('first\\nblah2\\nblah3')", "['blah2']");
135
136var regexp11 = /.*(?:blah).*$/;
137shouldBeNull("regexp11.exec('test')");
138shouldBe("regexp11.exec('blah')", "['blah']");
139shouldBe("regexp11.exec('1blah')", "['1blah']");
140shouldBe("regexp11.exec('blah1')", "['blah1']");
141shouldBe("regexp11.exec('blah blah blah')", "['blah blah blah']");
142shouldBeNull("regexp11.exec('blah\\nsecond')");
143shouldBe("regexp11.exec('first\\nblah')", "['blah']");
144shouldBeNull("regexp11.exec('first\\nblah\\nthird')");
145shouldBe("regexp11.exec('first\\nblah2\\nblah3')", "['blah3']");
146
147var regexp12 = /.*(?:blah|buzz|bang).*$/;
148shouldBeNull("regexp12.exec('test')");
149shouldBe("regexp12.exec('blah')", "['blah']");
150shouldBe("regexp12.exec('1blah')", "['1blah']");
151shouldBe("regexp12.exec('blah1')", "['blah1']");
152shouldBe("regexp12.exec('blah blah blah')", "['blah blah blah']");
153shouldBeNull("regexp12.exec('blah\\nsecond')");
154shouldBe("regexp12.exec('first\\nblah')", "['blah']");
155shouldBeNull("regexp12.exec('first\\nblah\\nthird')");
156shouldBe("regexp12.exec('first\\nblah2\\nblah3')", "['blah3']");
157
158var regexp13 = /.*\n\d+.*/;
159shouldBe("regexp13.exec('abc\\n123')", "['abc\\n123']");
160