]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_2/String/match-002.js
2 * File Name: String/match-002.js
3 * ECMA Section: 15.6.4.9
4 * Description: Based on ECMA 2 Draft 7 February 1999
6 * Author: christine@netscape.com
7 * Date: 19 February 1999
11 * String.match( regexp )
13 * If regexp is not an object of type RegExp, it is replaced with result
14 * of the expression new RegExp(regexp). Let string denote the result of
15 * converting the this value to a string. If regexp.global is false,
16 * return the result obtained by invoking RegExp.prototype.exec (see
17 * section 15.7.5.3) on regexp with string as parameter.
19 * Otherwise, set the regexp.lastIndex property to 0 and invoke
20 * RegExp.prototype.exec repeatedly until there is no match. If there is a
21 * match with an empty string (in other words, if the value of
22 * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
23 * The value returned is an array with the properties 0 through n-1
24 * corresponding to the first element of the result of each matching
25 * invocation of RegExp.prototype.exec.
27 * Note that the match function is intentionally generic; it does not
28 * require that its this value be a string object. Therefore, it can be
29 * transferred to other kinds of objects for use as a method.
31 * This file tests cases in which regexp.global is false. Therefore,
32 * results should behave as regexp.exec with string passed as a parameter.
36 var SECTION
= "String/match-002.js";
37 var VERSION
= "ECMA_2";
38 var TITLE
= "String.prototype.match( regexp )";
42 // the regexp argument is not a RegExp object
43 // this is not a string object
45 AddRegExpCases( /([\d
]{5})([-\ ]?[\d
]{4})?$/,
46 "/([\d]{5})([-\ ]?[\d]{4})?$/",
47 "Boston, Mass. 02134",
49 ["02134", "02134", undefined]);
51 AddGlobalRegExpCases( /([\d
]{5})([-\ ]?[\d
]{4})?$/g
,
52 "/([\d]{5})([-\ ]?[\d]{4})?$/g",
53 "Boston, Mass. 02134",
56 // set the value of lastIndex
57 re
= /([\d
]{5})([-\ ]?[\d
]{4})?$/;
60 s
= "Boston, MA 02134";
63 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex =0",
66 ["02134", "02134", undefined]);
69 re
.lastIndex
= s
.length
;
72 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
76 ["02134", "02134", undefined] );
78 re
.lastIndex
= s
.lastIndexOf("0");
81 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
85 ["02134", "02134", undefined]);
87 re
.lastIndex
= s
.lastIndexOf("0") + 1;
90 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
91 s
.lastIndexOf("0") +1,
94 ["02134", "02134", undefined]);
98 function AddRegExpCases(
99 regexp
, str_regexp
, string
, index
, matches_array
) {
101 // prevent a runtime error
103 if ( regexp
.exec(string
) == null || matches_array
== null ) {
105 string
+ ".match(" + regexp
+")",
107 string
.match(regexp
) );
113 "( " + string
+ " ).match(" + str_regexp
+").length",
114 matches_array
.length
,
115 string
.match(regexp
).length
);
118 "( " + string
+ " ).match(" + str_regexp
+").index",
120 string
.match(regexp
).index
);
123 "( " + string
+ " ).match(" + str_regexp
+").input",
125 string
.match(regexp
).input
);
127 var limit
= matches_array
.length
> string
.match(regexp
).length
?
128 matches_array
.length :
129 string
.match(regexp
).length
;
131 for ( var matches
= 0; matches
< limit
; matches
++ ) {
133 "( " + string
+ " ).match(" + str_regexp
+")[" + matches
+"]",
134 matches_array
[matches
],
135 string
.match(regexp
)[matches
] );
139 function AddGlobalRegExpCases(
140 regexp
, str_regexp
, string
, matches_array
) {
142 // prevent a runtime error
144 if ( regexp
.exec(string
) == null || matches_array
== null ) {
146 regexp
+ ".exec(" + string
+")",
148 regexp
.exec(string
) );
154 "( " + string
+ " ).match(" + str_regexp
+").length",
155 matches_array
.length
,
156 string
.match(regexp
).length
);
158 var limit
= matches_array
.length
> string
.match(regexp
).length
?
159 matches_array
.length :
160 string
.match(regexp
).length
;
162 for ( var matches
= 0; matches
< limit
; matches
++ ) {
164 "( " + string
+ " ).match(" + str_regexp
+")[" + matches
+"]",
165 matches_array
[matches
],
166 string
.match(regexp
)[matches
] );