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.
29 * @fileoverview Test String.prototype.match
33 function testMatch(name, input, regexp, result, captures, from, to) {
34 var matchResult = input.match(regexp);
35 assertEquals(result, matchResult, name + "-match");
37 var match = input.substring(from, to);
38 var preMatch = input.substring(0, from);
39 var postMatch = input.substring(to);
40 var lastParen = captures.length > 0 ? captures[captures.length - 1] : "";
43 // Returns array of matched strings.
44 var lastMatch = matchResult[matchResult.length - 1];
45 assertEquals(match, lastMatch, name + "-match-string_g");
47 // Returns array of match and captures.
48 assertEquals(match, matchResult[0], name + "-match-string");
49 assertEquals(captures.length + 1, matchResult.length, name + "-cap-return");
50 for (var i = 1; i < matchResult.length; i++) {
51 assertEquals(captures[i - 1], matchResult[i], name + "-cap-return-" + i);
55 assertEquals(match, RegExp["$&"], name + "-$&");
56 assertEquals(match, RegExp.lastMatch, name + "-lastMatch");
58 assertEquals(undefined, RegExp.$0, name + "-nocapture-10");
59 for (var i = 1; i <= 9; i++) {
60 if (i <= captures.length) {
61 assertEquals(captures[i - 1], RegExp["$" + i], name + "-capture-" + i);
63 assertEquals("", RegExp["$" + i], name + "-nocapture-" + i);
66 assertEquals(undefined, RegExp.$10, name + "-nocapture-10");
68 assertEquals(input, RegExp.input, name + "-input");
69 assertEquals(input, RegExp.$_, name + "-$_");
71 assertEquals(preMatch, RegExp["$`"], name + "-$`");
72 assertEquals(preMatch, RegExp.leftContext, name + "-leftContex");
74 assertEquals(postMatch, RegExp["$'"], name + "-$'");
75 assertEquals(postMatch, RegExp.rightContext, name + "-rightContex");
77 assertEquals(lastParen, RegExp["$+"], name + "-$+");
78 assertEquals(lastParen, RegExp.lastParen, name + "-lastParen");
83 var stringSample = "A man, a plan, a canal: Panama";
84 var stringSample2 = "Argle bargle glop glyf!";
85 var stringSample3 = "abcdefghijxxxxxxxxxx";
88 // Non-capturing, non-global regexp.
92 testMatch("Nonglobal", stringSample, re_nog,
99 testMatch("Nonglobal-ignore-lastIndex", stringSample, re_nog,
104 // Capturing non-global regexp.
106 var re_multicap = /(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/;
107 testMatch("Capture-Nonglobal", stringSample3, re_multicap,
108 ["abcdefghij", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
109 ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"], 0, 10);
112 // Global regexp (also check that capture from before are cleared)
115 testMatch("Global", stringSample2, re,
116 ["Argle", "bargle", "glop", "glyf"], [], 18, 22);
120 testMatch("Global-ignore-lastIndex", stringSample2, re,
121 ["Argle", "bargle", "glop", "glyf"], [], 18, 22);
124 // Capturing global regexp
126 var re_cap = /\w(\w*)/g;
128 testMatch("Capture-Global", stringSample, re_cap,
129 ["A", "man", "a", "plan", "a", "canal", "Panama"],
137 testMatch("Atom", stringSample, re_atom,
144 var re_atomg = /an/g;
146 testMatch("Global-Atom", stringSample, re_atomg,
147 ["an", "an", "an", "an"],