|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 * File Name: String/match-002.js |
|
9 * ECMA Section: 15.6.4.9 |
|
10 * Description: Based on ECMA 2 Draft 7 February 1999 |
|
11 * |
|
12 * Author: christine@netscape.com |
|
13 * Date: 19 February 1999 |
|
14 */ |
|
15 |
|
16 /* |
|
17 * String.match( regexp ) |
|
18 * |
|
19 * If regexp is not an object of type RegExp, it is replaced with result |
|
20 * of the expression new RegExp(regexp). Let string denote the result of |
|
21 * converting the this value to a string. If regexp.global is false, |
|
22 * return the result obtained by invoking RegExp.prototype.exec (see |
|
23 * section 15.7.5.3) on regexp with string as parameter. |
|
24 * |
|
25 * Otherwise, set the regexp.lastIndex property to 0 and invoke |
|
26 * RegExp.prototype.exec repeatedly until there is no match. If there is a |
|
27 * match with an empty string (in other words, if the value of |
|
28 * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1. |
|
29 * The value returned is an array with the properties 0 through n-1 |
|
30 * corresponding to the first element of the result of each matching |
|
31 * invocation of RegExp.prototype.exec. |
|
32 * |
|
33 * Note that the match function is intentionally generic; it does not |
|
34 * require that its this value be a string object. Therefore, it can be |
|
35 * transferred to other kinds of objects for use as a method. |
|
36 * |
|
37 * This file tests cases in which regexp.global is false. Therefore, |
|
38 * results should behave as regexp.exec with string passed as a parameter. |
|
39 * |
|
40 */ |
|
41 |
|
42 var SECTION = "String/match-002.js"; |
|
43 var VERSION = "ECMA_2"; |
|
44 var TITLE = "String.prototype.match( regexp )"; |
|
45 |
|
46 startTest(); |
|
47 |
|
48 // the regexp argument is not a RegExp object |
|
49 // this is not a string object |
|
50 |
|
51 AddRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/, |
|
52 "/([\d]{5})([-\ ]?[\d]{4})?$/", |
|
53 "Boston, Mass. 02134", |
|
54 14, |
|
55 ["02134", "02134", undefined]); |
|
56 |
|
57 AddGlobalRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/g, |
|
58 "/([\d]{5})([-\ ]?[\d]{4})?$/g", |
|
59 "Boston, Mass. 02134", |
|
60 ["02134"]); |
|
61 |
|
62 // set the value of lastIndex |
|
63 re = /([\d]{5})([-\ ]?[\d]{4})?$/; |
|
64 re.lastIndex = 0; |
|
65 |
|
66 s = "Boston, MA 02134"; |
|
67 |
|
68 AddRegExpCases( re, |
|
69 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex =0", |
|
70 s, |
|
71 s.lastIndexOf("0"), |
|
72 ["02134", "02134", undefined]); |
|
73 |
|
74 |
|
75 re.lastIndex = s.length; |
|
76 |
|
77 AddRegExpCases( re, |
|
78 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
|
79 s.length, |
|
80 s, |
|
81 s.lastIndexOf("0"), |
|
82 ["02134", "02134", undefined] ); |
|
83 |
|
84 re.lastIndex = s.lastIndexOf("0"); |
|
85 |
|
86 AddRegExpCases( re, |
|
87 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
|
88 s.lastIndexOf("0"), |
|
89 s, |
|
90 s.lastIndexOf("0"), |
|
91 ["02134", "02134", undefined]); |
|
92 |
|
93 re.lastIndex = s.lastIndexOf("0") + 1; |
|
94 |
|
95 AddRegExpCases( re, |
|
96 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
|
97 s.lastIndexOf("0") +1, |
|
98 s, |
|
99 s.lastIndexOf("0"), |
|
100 ["02134", "02134", undefined]); |
|
101 |
|
102 test(); |
|
103 |
|
104 function AddRegExpCases( |
|
105 regexp, str_regexp, string, index, matches_array ) { |
|
106 |
|
107 // prevent a runtime error |
|
108 |
|
109 if ( regexp.exec(string) == null || matches_array == null ) { |
|
110 AddTestCase( |
|
111 string + ".match(" + regexp +")", |
|
112 matches_array, |
|
113 string.match(regexp) ); |
|
114 |
|
115 return; |
|
116 } |
|
117 |
|
118 AddTestCase( |
|
119 "( " + string + " ).match(" + str_regexp +").length", |
|
120 matches_array.length, |
|
121 string.match(regexp).length ); |
|
122 |
|
123 AddTestCase( |
|
124 "( " + string + " ).match(" + str_regexp +").index", |
|
125 index, |
|
126 string.match(regexp).index ); |
|
127 |
|
128 AddTestCase( |
|
129 "( " + string + " ).match(" + str_regexp +").input", |
|
130 string, |
|
131 string.match(regexp).input ); |
|
132 |
|
133 var limit = matches_array.length > string.match(regexp).length ? |
|
134 matches_array.length : |
|
135 string.match(regexp).length; |
|
136 |
|
137 for ( var matches = 0; matches < limit; matches++ ) { |
|
138 AddTestCase( |
|
139 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", |
|
140 matches_array[matches], |
|
141 string.match(regexp)[matches] ); |
|
142 } |
|
143 } |
|
144 |
|
145 function AddGlobalRegExpCases( |
|
146 regexp, str_regexp, string, matches_array ) { |
|
147 |
|
148 // prevent a runtime error |
|
149 |
|
150 if ( regexp.exec(string) == null || matches_array == null ) { |
|
151 AddTestCase( |
|
152 regexp + ".exec(" + string +")", |
|
153 matches_array, |
|
154 regexp.exec(string) ); |
|
155 |
|
156 return; |
|
157 } |
|
158 |
|
159 AddTestCase( |
|
160 "( " + string + " ).match(" + str_regexp +").length", |
|
161 matches_array.length, |
|
162 string.match(regexp).length ); |
|
163 |
|
164 var limit = matches_array.length > string.match(regexp).length ? |
|
165 matches_array.length : |
|
166 string.match(regexp).length; |
|
167 |
|
168 for ( var matches = 0; matches < limit; matches++ ) { |
|
169 AddTestCase( |
|
170 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", |
|
171 matches_array[matches], |
|
172 string.match(regexp)[matches] ); |
|
173 } |
|
174 } |