2 * File Name: String/match-003.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 var SECTION = "String/match-003.js";
33 var VERSION = "ECMA_2";
34 var TITLE = "String.prototype.match( regexp )";
38 // the regexp argument is not a RegExp object
39 // this is not a string object
42 // [if regexp.global is true] set the regexp.lastIndex property to 0 and
43 // invoke RegExp.prototype.exec repeatedly until there is no match. If
44 // there is a match with an empty string (in other words, if the value of
45 // regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
46 // The value returned is an array with the properties 0 through n-1
47 // corresponding to the first element of the result of each matching invocation
48 // of RegExp.prototype.exec.
51 // set the value of lastIndex
52 re = /([\d]{5})([-\ ]?[\d]{4})?$/g;
55 s = "Boston, MA 02134";
57 AddGlobalRegExpCases( re,
66 "re = " + re + "; re.lastIndex = 0 ",
71 re.lastIndex = s.length;
75 "re = " + re + "; re.lastIndex = " + s.length,
79 re.lastIndex = s.lastIndexOf("0");
83 "re = "+ re +"; re.lastIndex = " + s.lastIndexOf("0"),
87 re.lastIndex = s.lastIndexOf("0") + 1;
91 "re = " +re+ "; re.lastIndex = " + (s.lastIndexOf("0") +1),
97 function AddGlobalRegExpCases(
98 regexp, str_regexp, string, matches_array ) {
100 // prevent a runtime error
102 if ( string.match(regexp) == null || matches_array == null ) {
104 string + ".match(" + str_regexp +")",
106 string.match(regexp) );
112 "( " + string + " ).match(" + str_regexp +").length",
113 matches_array.length,
114 string.match(regexp).length );
116 var limit = matches_array.length > string.match(regexp).length ?
117 matches_array.length :
118 string.match(regexp).length;
120 for ( var matches = 0; matches < limit; matches++ ) {
122 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
123 matches_array[matches],
124 string.match(regexp)[matches] );