michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * File Name: String/match-001.js michael@0: * ECMA Section: 15.6.4.9 michael@0: * Description: Based on ECMA 2 Draft 7 February 1999 michael@0: * michael@0: * Author: christine@netscape.com michael@0: * Date: 19 February 1999 michael@0: */ michael@0: michael@0: /* michael@0: * String.match( regexp ) michael@0: * michael@0: * If regexp is not an object of type RegExp, it is replaced with result michael@0: * of the expression new RegExp(regexp). Let string denote the result of michael@0: * converting the this value to a string. If regexp.global is false, michael@0: * return the result obtained by invoking RegExp.prototype.exec (see michael@0: * section 15.7.5.3) on regexp with string as parameter. michael@0: * michael@0: * Otherwise, set the regexp.lastIndex property to 0 and invoke michael@0: * RegExp.prototype.exec repeatedly until there is no match. If there is a michael@0: * match with an empty string (in other words, if the value of michael@0: * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1. michael@0: * The value returned is an array with the properties 0 through n-1 michael@0: * corresponding to the first element of the result of each matching michael@0: * invocation of RegExp.prototype.exec. michael@0: * michael@0: * Note that the match function is intentionally generic; it does not michael@0: * require that its this value be a string object. Therefore, it can be michael@0: * transferred to other kinds of objects for use as a method. michael@0: */ michael@0: michael@0: var SECTION = "String/match-001.js"; michael@0: var VERSION = "ECMA_2"; michael@0: var TITLE = "String.prototype.match( regexp )"; michael@0: michael@0: startTest(); michael@0: michael@0: // the regexp argument is not a RegExp object michael@0: // this is not a string object michael@0: michael@0: // cases in which the regexp global property is false michael@0: michael@0: AddRegExpCases( 3, "3", "1234567890", 1, 2, ["3"] ); michael@0: michael@0: // cases in which the regexp object global property is true michael@0: michael@0: AddGlobalRegExpCases( /34/g, "/34/g", "343443444", 3, ["34", "34", "34"] ); michael@0: AddGlobalRegExpCases( /\d{1}/g, "/d{1}/g", "123456abcde7890", 10, michael@0: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] ); michael@0: michael@0: AddGlobalRegExpCases( /\d{2}/g, "/d{2}/g", "123456abcde7890", 5, michael@0: ["12", "34", "56", "78", "90"] ); michael@0: michael@0: AddGlobalRegExpCases( /\D{2}/g, "/d{2}/g", "123456abcde7890", 2, michael@0: ["ab", "cd"] ); michael@0: michael@0: test(); michael@0: michael@0: michael@0: function AddRegExpCases( michael@0: regexp, str_regexp, string, length, index, matches_array ) { michael@0: michael@0: AddTestCase( michael@0: "( " + string + " ).match(" + str_regexp +").length", michael@0: length, michael@0: string.match(regexp).length ); michael@0: michael@0: AddTestCase( michael@0: "( " + string + " ).match(" + str_regexp +").index", michael@0: index, michael@0: string.match(regexp).index ); michael@0: michael@0: AddTestCase( michael@0: "( " + string + " ).match(" + str_regexp +").input", michael@0: string, michael@0: string.match(regexp).input ); michael@0: michael@0: for ( var matches = 0; matches < matches_array.length; matches++ ) { michael@0: AddTestCase( michael@0: "( " + string + " ).match(" + str_regexp +")[" + matches +"]", michael@0: matches_array[matches], michael@0: string.match(regexp)[matches] ); michael@0: } michael@0: } michael@0: michael@0: function AddGlobalRegExpCases( michael@0: regexp, str_regexp, string, length, matches_array ) { michael@0: michael@0: AddTestCase( michael@0: "( " + string + " ).match(" + str_regexp +").length", michael@0: length, michael@0: string.match(regexp).length ); michael@0: michael@0: for ( var matches = 0; matches < matches_array.length; matches++ ) { michael@0: AddTestCase( michael@0: "( " + string + " ).match(" + str_regexp +")[" + matches +"]", michael@0: matches_array[matches], michael@0: string.match(regexp)[matches] ); michael@0: } michael@0: }