1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * File Name: String/match-002.js
4 * ECMA Section: 15.6.4.9
5 * Description: Based on ECMA 2 Draft 7 February 1999
7 * Author: christine@netscape.com
8 * Date: 19 February 1999
12 * String.match( regexp )
14 * If regexp is not an object of type RegExp, it is replaced with result
15 * of the expression new RegExp(regexp). Let string denote the result of
16 * converting the this value to a string. If regexp.global is false,
17 * return the result obtained by invoking RegExp.prototype.exec (see
18 * section 15.7.5.3) on regexp with string as parameter.
20 * Otherwise, set the regexp.lastIndex property to 0 and invoke
21 * RegExp.prototype.exec repeatedly until there is no match. If there is a
22 * match with an empty string (in other words, if the value of
23 * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
24 * The value returned is an array with the properties 0 through n-1
25 * corresponding to the first element of the result of each matching
26 * invocation of RegExp.prototype.exec.
28 * Note that the match function is intentionally generic; it does not
29 * require that its this value be a string object. Therefore, it can be
30 * transferred to other kinds of objects for use as a method.
32 * This file tests cases in which regexp.global is false. Therefore,
33 * results should behave as regexp.exec with string passed as a parameter.
37 var SECTION = "String/match-002.js";
38 var VERSION = "ECMA_2";
39 var TITLE = "String.prototype.match( regexp )";
43 // the regexp argument is not a RegExp object
44 // this is not a string object
46 AddRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/,
47 "/([\d]{5})([-\ ]?[\d]{4})?$/",
48 "Boston, Mass. 02134",
50 ["02134", "02134", undefined]);
52 AddGlobalRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/g,
53 "/([\d]{5})([-\ ]?[\d]{4})?$/g",
54 "Boston, Mass. 02134",
57 // set the value of lastIndex
58 re = /([\d]{5})([-\ ]?[\d]{4})?$/;
61 s = "Boston, MA 02134";
64 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex =0",
67 ["02134", "02134", undefined]);
70 re.lastIndex = s.length;
73 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
77 ["02134", "02134", undefined] );
79 re.lastIndex = s.lastIndexOf("0");
82 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
86 ["02134", "02134", undefined]);
88 re.lastIndex = s.lastIndexOf("0") + 1;
91 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
92 s.lastIndexOf("0") +1,
95 ["02134", "02134", undefined]);
99 function AddRegExpCases(
100 regexp, str_regexp, string, index, matches_array ) {
102 // prevent a runtime error
104 if ( regexp.exec(string) == null || matches_array == null ) {
106 string + ".match(" + regexp +")",
108 string.match(regexp) );
114 "( " + string + " ).match(" + str_regexp +").length",
115 matches_array.length,
116 string.match(regexp).length );
119 "( " + string + " ).match(" + str_regexp +").index",
121 string.match(regexp).index );
124 "( " + string + " ).match(" + str_regexp +").input",
126 string.match(regexp).input );
128 var limit = matches_array.length > string.match(regexp).length ?
129 matches_array.length :
130 string.match(regexp).length;
132 for ( var matches = 0; matches < limit; matches++ ) {
134 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
135 matches_array[matches],
136 string.match(regexp)[matches] );
140 function AddGlobalRegExpCases(
141 regexp, str_regexp, string, matches_array ) {
143 // prevent a runtime error
145 if ( regexp.exec(string) == null || matches_array == null ) {
147 regexp + ".exec(" + string +")",
149 regexp.exec(string) );
155 "( " + string + " ).match(" + str_regexp +").length",
156 matches_array.length,
157 string.match(regexp).length );
159 var limit = matches_array.length > string.match(regexp).length ?
160 matches_array.length :
161 string.match(regexp).length;
163 for ( var matches = 0; matches < limit; matches++ ) {
165 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
166 matches_array[matches],
167 string.match(regexp)[matches] );