1/** 2 * File Name: String/match-003.js 3 * ECMA Section: 15.6.4.9 4 * Description: Based on ECMA 2 Draft 7 February 1999 5 * 6 * Author: christine@netscape.com 7 * Date: 19 February 1999 8 */ 9 10/* 11 * String.match( regexp ) 12 * 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. 18 * 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. 26 * 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. 30 */ 31 32 var SECTION = "String/match-003.js"; 33 var VERSION = "ECMA_2"; 34 var TITLE = "String.prototype.match( regexp )"; 35 36 startTest(); 37 38 // the regexp argument is not a RegExp object 39 // this is not a string object 40 41 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. 49 50 51 // set the value of lastIndex 52 re = /([\d]{5})([-\ ]?[\d]{4})?$/g; 53 54 55 s = "Boston, MA 02134"; 56 57 AddGlobalRegExpCases( re, 58 "re = " + re, 59 s, 60 ["02134" ]); 61 62 re.lastIndex = 0; 63 64 AddGlobalRegExpCases( 65 re, 66 "re = " + re + "; re.lastIndex = 0 ", 67 s, 68 ["02134"]); 69 70 71 re.lastIndex = s.length; 72 73 AddGlobalRegExpCases( 74 re, 75 "re = " + re + "; re.lastIndex = " + s.length, 76 s, 77 ["02134"] ); 78 79 re.lastIndex = s.lastIndexOf("0"); 80 81 AddGlobalRegExpCases( 82 re, 83 "re = "+ re +"; re.lastIndex = " + s.lastIndexOf("0"), 84 s, 85 ["02134"]); 86 87 re.lastIndex = s.lastIndexOf("0") + 1; 88 89 AddGlobalRegExpCases( 90 re, 91 "re = " +re+ "; re.lastIndex = " + (s.lastIndexOf("0") +1), 92 s, 93 ["02134"]); 94 95 test(); 96 97function AddGlobalRegExpCases( 98 regexp, str_regexp, string, matches_array ) { 99 100 // prevent a runtime error 101 102 if ( string.match(regexp) == null || matches_array == null ) { 103 AddTestCase( 104 string + ".match(" + str_regexp +")", 105 matches_array, 106 string.match(regexp) ); 107 108 return; 109 } 110 111 AddTestCase( 112 "( " + string + " ).match(" + str_regexp +").length", 113 matches_array.length, 114 string.match(regexp).length ); 115 116 var limit = matches_array.length > string.match(regexp).length ? 117 matches_array.length : 118 string.match(regexp).length; 119 120 for ( var matches = 0; matches < limit; matches++ ) { 121 AddTestCase( 122 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 123 matches_array[matches], 124 string.match(regexp)[matches] ); 125 } 126} 127