match-002.js (4820B)
1 /* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 7 /** 8 * File Name: String/match-002.js 9 * ECMA Section: 15.6.4.9 10 * Description: Based on ECMA 2 Draft 7 February 1999 11 * 12 * Author: christine@netscape.com 13 * Date: 19 February 1999 14 */ 15 16 /* 17 * String.match( regexp ) 18 * 19 * If regexp is not an object of type RegExp, it is replaced with result 20 * of the expression new RegExp(regexp). Let string denote the result of 21 * converting the this value to a string. If regexp.global is false, 22 * return the result obtained by invoking RegExp.prototype.exec (see 23 * section 15.7.5.3) on regexp with string as parameter. 24 * 25 * Otherwise, set the regexp.lastIndex property to 0 and invoke 26 * RegExp.prototype.exec repeatedly until there is no match. If there is a 27 * match with an empty string (in other words, if the value of 28 * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1. 29 * The value returned is an array with the properties 0 through n-1 30 * corresponding to the first element of the result of each matching 31 * invocation of RegExp.prototype.exec. 32 * 33 * Note that the match function is intentionally generic; it does not 34 * require that its this value be a string object. Therefore, it can be 35 * transferred to other kinds of objects for use as a method. 36 * 37 * This file tests cases in which regexp.global is false. Therefore, 38 * results should behave as regexp.exec with string passed as a parameter. 39 * 40 */ 41 42 var SECTION = "String/match-002.js"; 43 var TITLE = "String.prototype.match( regexp )"; 44 45 46 // the regexp argument is not a RegExp object 47 // this is not a string object 48 49 AddRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/, 50 "/([\d]{5})([-\ ]?[\d]{4})?$/", 51 "Boston, Mass. 02134", 52 14, 53 ["02134", "02134", undefined]); 54 55 AddGlobalRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/g, 56 "/([\d]{5})([-\ ]?[\d]{4})?$/g", 57 "Boston, Mass. 02134", 58 ["02134"]); 59 60 // set the value of lastIndex 61 re = /([\d]{5})([-\ ]?[\d]{4})?$/; 62 re.lastIndex = 0; 63 64 s = "Boston, MA 02134"; 65 66 AddRegExpCases( re, 67 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex =0", 68 s, 69 s.lastIndexOf("0"), 70 ["02134", "02134", undefined]); 71 72 73 re.lastIndex = s.length; 74 75 AddRegExpCases( re, 76 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 77 s.length, 78 s, 79 s.lastIndexOf("0"), 80 ["02134", "02134", undefined] ); 81 82 re.lastIndex = s.lastIndexOf("0"); 83 84 AddRegExpCases( re, 85 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 86 s.lastIndexOf("0"), 87 s, 88 s.lastIndexOf("0"), 89 ["02134", "02134", undefined]); 90 91 re.lastIndex = s.lastIndexOf("0") + 1; 92 93 AddRegExpCases( re, 94 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 95 s.lastIndexOf("0") +1, 96 s, 97 s.lastIndexOf("0"), 98 ["02134", "02134", undefined]); 99 100 test(); 101 102 function AddRegExpCases( 103 regexp, str_regexp, string, index, matches_array ) { 104 105 // prevent a runtime error 106 107 if ( regexp.exec(string) == null || matches_array == null ) { 108 AddTestCase( 109 string + ".match(" + regexp +")", 110 matches_array, 111 string.match(regexp) ); 112 113 return; 114 } 115 116 AddTestCase( 117 "( " + string + " ).match(" + str_regexp +").length", 118 matches_array.length, 119 string.match(regexp).length ); 120 121 AddTestCase( 122 "( " + string + " ).match(" + str_regexp +").index", 123 index, 124 string.match(regexp).index ); 125 126 AddTestCase( 127 "( " + string + " ).match(" + str_regexp +").input", 128 string, 129 string.match(regexp).input ); 130 131 var limit = matches_array.length > string.match(regexp).length ? 132 matches_array.length : 133 string.match(regexp).length; 134 135 for ( var matches = 0; matches < limit; matches++ ) { 136 AddTestCase( 137 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 138 matches_array[matches], 139 string.match(regexp)[matches] ); 140 } 141 } 142 143 function AddGlobalRegExpCases( 144 regexp, str_regexp, string, matches_array ) { 145 146 // prevent a runtime error 147 148 if ( regexp.exec(string) == null || matches_array == null ) { 149 AddTestCase( 150 regexp + ".exec(" + string +")", 151 matches_array, 152 regexp.exec(string) ); 153 154 return; 155 } 156 157 AddTestCase( 158 "( " + string + " ).match(" + str_regexp +").length", 159 matches_array.length, 160 string.match(regexp).length ); 161 162 var limit = matches_array.length > string.match(regexp).length ? 163 matches_array.length : 164 string.match(regexp).length; 165 166 for ( var matches = 0; matches < limit; matches++ ) { 167 AddTestCase( 168 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 169 matches_array[matches], 170 string.match(regexp)[matches] ); 171 } 172 }