1 /** 2 * File Name: String/match-001.js 3 * ECMA Section: 15.6.4.9 4 * Description: Based on ECMA 2 Draft 7 February 1999 5 * 6 * Author: christine (at) 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-001.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 // cases in which the regexp global property is false 42 43 AddRegExpCases( 3, "3", "1234567890", 1, 2, ["3"] ); 44 45 // cases in which the regexp object global property is true 46 47 AddGlobalRegExpCases( /34/g, "/34/g", "343443444", 3, ["34", "34", "34"] ); 48 AddGlobalRegExpCases( /\d{1}/g, "/d{1}/g", "123456abcde7890", 10, 49 ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] ); 50 51 AddGlobalRegExpCases( /\d{2}/g, "/d{2}/g", "123456abcde7890", 5, 52 ["12", "34", "56", "78", "90"] ); 53 54 AddGlobalRegExpCases( /\D{2}/g, "/d{2}/g", "123456abcde7890", 2, 55 ["ab", "cd"] ); 56 57 test(); 58 59 60 function AddRegExpCases( 61 regexp, str_regexp, string, length, index, matches_array ) { 62 63 AddTestCase( 64 "( " + string + " ).match(" + str_regexp +").length", 65 length, 66 string.match(regexp).length ); 67 68 AddTestCase( 69 "( " + string + " ).match(" + str_regexp +").index", 70 index, 71 string.match(regexp).index ); 72 73 AddTestCase( 74 "( " + string + " ).match(" + str_regexp +").input", 75 string, 76 string.match(regexp).input ); 77 78 for ( var matches = 0; matches < matches_array.length; matches++ ) { 79 AddTestCase( 80 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 81 matches_array[matches], 82 string.match(regexp)[matches] ); 83 } 84 } 85 86 function AddGlobalRegExpCases( 87 regexp, str_regexp, string, length, matches_array ) { 88 89 AddTestCase( 90 "( " + string + " ).match(" + str_regexp +").length", 91 length, 92 string.match(regexp).length ); 93 94 for ( var matches = 0; matches < matches_array.length; matches++ ) { 95 AddTestCase( 96 "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 97 matches_array[matches], 98 string.match(regexp)[matches] ); 99 } 100 } 101