2 * File Name: String/match-004.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.
32 * The match function should be intentionally generic, and not require
33 * this to be a string.
37 var SECTION = "String/match-004.js";
38 var VERSION = "ECMA_2";
39 var TITLE = "String.prototype.match( regexp )";
41 var BUGNUMBER="http://scopus/bugsplat/show_bug.cgi?id=345818";
45 // set the value of lastIndex
47 s = 10203040506070809000;
49 Number.prototype.match = String.prototype.match;
61 "re = " + re +" [lastIndex is " + re.lastIndex+"]",
68 re.lastIndex = s.length;
71 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
77 re.lastIndex = s.lastIndexOf("0");
80 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
86 re.lastIndex = s.lastIndexOf("0") + 1;
89 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
90 s.lastIndexOf("0") +1,
97 function AddRegExpCases(
98 regexp, str_regexp, string, str_string, index, matches_array ) {
100 // prevent a runtime error
102 if ( regexp.exec(string) == null || matches_array == null ) {
104 string + ".match(" + regexp +")",
106 string.match(regexp) );
112 "( " + string + " ).match(" + str_regexp +").length",
113 matches_array.length,
114 string.match(regexp).length );
117 "( " + string + " ).match(" + str_regexp +").index",
119 string.match(regexp).index );
122 "( " + string + " ).match(" + str_regexp +").input",
124 string.match(regexp).input );
126 var limit = matches_array.length > string.match(regexp).length ?
127 matches_array.length :
128 string.match(regexp).length;
130 for ( var matches = 0; matches < limit; matches++ ) {
132 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
133 matches_array[matches],
134 string.match(regexp)[matches] );
138 function AddGlobalRegExpCases(
139 regexp, str_regexp, string, matches_array ) {
141 // prevent a runtime error
143 if ( regexp.exec(string) == null || matches_array == null ) {
145 regexp + ".exec(" + string +")",
147 regexp.exec(string) );
153 "( " + string + " ).match(" + str_regexp +").length",
154 matches_array.length,
155 string.match(regexp).length );
157 var limit = matches_array.length > string.match(regexp).length ?
158 matches_array.length :
159 string.match(regexp).length;
161 for ( var matches = 0; matches < limit; matches++ ) {
163 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
164 matches_array[matches],
165 string.match(regexp)[matches] );