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