|
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-004.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 * |
|
38 * The match function should be intentionally generic, and not require |
|
39 * this to be a string. |
|
40 * |
|
41 */ |
|
42 |
|
43 var SECTION = "String/match-004.js"; |
|
44 var VERSION = "ECMA_2"; |
|
45 var TITLE = "String.prototype.match( regexp )"; |
|
46 |
|
47 var BUGNUMBER="http://scopus/bugsplat/show_bug.cgi?id=345818"; |
|
48 |
|
49 startTest(); |
|
50 |
|
51 // set the value of lastIndex |
|
52 re = /0./; |
|
53 s = 10203040506070809000; |
|
54 |
|
55 Number.prototype.match = String.prototype.match; |
|
56 |
|
57 AddRegExpCases( re, |
|
58 "re = " + re , |
|
59 s, |
|
60 String(s), |
|
61 1, |
|
62 ["02"]); |
|
63 |
|
64 |
|
65 re.lastIndex = 0; |
|
66 AddRegExpCases( re, |
|
67 "re = " + re +" [lastIndex is " + re.lastIndex+"]", |
|
68 s, |
|
69 String(s), |
|
70 1, |
|
71 ["02"]); |
|
72 /* |
|
73 |
|
74 re.lastIndex = s.length; |
|
75 |
|
76 AddRegExpCases( re, |
|
77 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
|
78 s.length, |
|
79 s, |
|
80 s.lastIndexOf("0"), |
|
81 null ); |
|
82 |
|
83 re.lastIndex = s.lastIndexOf("0"); |
|
84 |
|
85 AddRegExpCases( re, |
|
86 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
|
87 s.lastIndexOf("0"), |
|
88 s, |
|
89 s.lastIndexOf("0"), |
|
90 ["02134"]); |
|
91 |
|
92 re.lastIndex = s.lastIndexOf("0") + 1; |
|
93 |
|
94 AddRegExpCases( re, |
|
95 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
|
96 s.lastIndexOf("0") +1, |
|
97 s, |
|
98 0, |
|
99 null); |
|
100 */ |
|
101 test(); |
|
102 |
|
103 function AddRegExpCases( |
|
104 regexp, str_regexp, string, str_string, index, matches_array ) { |
|
105 |
|
106 // prevent a runtime error |
|
107 |
|
108 if ( regexp.exec(string) == null || matches_array == null ) { |
|
109 AddTestCase( |
|
110 string + ".match(" + regexp +")", |
|
111 matches_array, |
|
112 string.match(regexp) ); |
|
113 |
|
114 return; |
|
115 } |
|
116 |
|
117 AddTestCase( |
|
118 "( " + string + " ).match(" + str_regexp +").length", |
|
119 matches_array.length, |
|
120 string.match(regexp).length ); |
|
121 |
|
122 AddTestCase( |
|
123 "( " + string + " ).match(" + str_regexp +").index", |
|
124 index, |
|
125 string.match(regexp).index ); |
|
126 |
|
127 AddTestCase( |
|
128 "( " + string + " ).match(" + str_regexp +").input", |
|
129 str_string, |
|
130 string.match(regexp).input ); |
|
131 |
|
132 var limit = matches_array.length > string.match(regexp).length ? |
|
133 matches_array.length : |
|
134 string.match(regexp).length; |
|
135 |
|
136 for ( var matches = 0; matches < limit; matches++ ) { |
|
137 AddTestCase( |
|
138 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", |
|
139 matches_array[matches], |
|
140 string.match(regexp)[matches] ); |
|
141 } |
|
142 } |
|
143 |
|
144 function AddGlobalRegExpCases( |
|
145 regexp, str_regexp, string, matches_array ) { |
|
146 |
|
147 // prevent a runtime error |
|
148 |
|
149 if ( regexp.exec(string) == null || matches_array == null ) { |
|
150 AddTestCase( |
|
151 regexp + ".exec(" + string +")", |
|
152 matches_array, |
|
153 regexp.exec(string) ); |
|
154 |
|
155 return; |
|
156 } |
|
157 |
|
158 AddTestCase( |
|
159 "( " + string + " ).match(" + str_regexp +").length", |
|
160 matches_array.length, |
|
161 string.match(regexp).length ); |
|
162 |
|
163 var limit = matches_array.length > string.match(regexp).length ? |
|
164 matches_array.length : |
|
165 string.match(regexp).length; |
|
166 |
|
167 for ( var matches = 0; matches < limit; matches++ ) { |
|
168 AddTestCase( |
|
169 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", |
|
170 matches_array[matches], |
|
171 string.match(regexp)[matches] ); |
|
172 } |
|
173 } |