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-003.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-003.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: michael@0: // [if regexp.global is true] set the regexp.lastIndex property to 0 and michael@0: // invoke RegExp.prototype.exec repeatedly until there is no match. If michael@0: // there is a 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 invocation michael@0: // of RegExp.prototype.exec. michael@0: michael@0: michael@0: // set the value of lastIndex michael@0: re = /([\d]{5})([-\ ]?[\d]{4})?$/g; michael@0: michael@0: michael@0: s = "Boston, MA 02134"; michael@0: michael@0: AddGlobalRegExpCases( re, michael@0: "re = " + re, michael@0: s, michael@0: ["02134" ]); michael@0: michael@0: re.lastIndex = 0; michael@0: michael@0: AddGlobalRegExpCases( michael@0: re, michael@0: "re = " + re + "; re.lastIndex = 0 ", michael@0: s, michael@0: ["02134"]); michael@0: michael@0: michael@0: re.lastIndex = s.length; michael@0: michael@0: AddGlobalRegExpCases( michael@0: re, michael@0: "re = " + re + "; re.lastIndex = " + s.length, michael@0: s, michael@0: ["02134"] ); michael@0: michael@0: re.lastIndex = s.lastIndexOf("0"); michael@0: michael@0: AddGlobalRegExpCases( michael@0: re, michael@0: "re = "+ re +"; re.lastIndex = " + s.lastIndexOf("0"), michael@0: s, michael@0: ["02134"]); michael@0: michael@0: re.lastIndex = s.lastIndexOf("0") + 1; michael@0: michael@0: AddGlobalRegExpCases( michael@0: re, michael@0: "re = " +re+ "; re.lastIndex = " + (s.lastIndexOf("0") +1), michael@0: s, michael@0: ["02134"]); michael@0: michael@0: test(); michael@0: michael@0: function AddGlobalRegExpCases( michael@0: regexp, str_regexp, string, matches_array ) { michael@0: michael@0: // prevent a runtime error michael@0: michael@0: if ( string.match(regexp) == null || matches_array == null ) { michael@0: AddTestCase( michael@0: string + ".match(" + str_regexp +")", michael@0: matches_array, michael@0: string.match(regexp) ); michael@0: michael@0: return; michael@0: } michael@0: michael@0: AddTestCase( michael@0: "( " + string + " ).match(" + str_regexp +").length", michael@0: matches_array.length, michael@0: string.match(regexp).length ); michael@0: michael@0: var limit = matches_array.length > string.match(regexp).length ? michael@0: matches_array.length : michael@0: string.match(regexp).length; michael@0: michael@0: for ( var matches = 0; matches < limit; 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: }