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-004.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: * The match function should be intentionally generic, and not require michael@0: * this to be a string. michael@0: * michael@0: */ michael@0: michael@0: var SECTION = "String/match-004.js"; michael@0: var VERSION = "ECMA_2"; michael@0: var TITLE = "String.prototype.match( regexp )"; michael@0: michael@0: var BUGNUMBER="http://scopus/bugsplat/show_bug.cgi?id=345818"; michael@0: michael@0: startTest(); michael@0: michael@0: // set the value of lastIndex michael@0: re = /0./; michael@0: s = 10203040506070809000; michael@0: michael@0: Number.prototype.match = String.prototype.match; michael@0: michael@0: AddRegExpCases( re, michael@0: "re = " + re , michael@0: s, michael@0: String(s), michael@0: 1, michael@0: ["02"]); michael@0: michael@0: michael@0: re.lastIndex = 0; michael@0: AddRegExpCases( re, michael@0: "re = " + re +" [lastIndex is " + re.lastIndex+"]", michael@0: s, michael@0: String(s), michael@0: 1, michael@0: ["02"]); michael@0: /* michael@0: michael@0: re.lastIndex = s.length; michael@0: michael@0: AddRegExpCases( re, michael@0: "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + michael@0: s.length, michael@0: s, michael@0: s.lastIndexOf("0"), michael@0: null ); michael@0: michael@0: re.lastIndex = s.lastIndexOf("0"); michael@0: michael@0: AddRegExpCases( re, michael@0: "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + michael@0: s.lastIndexOf("0"), michael@0: s, michael@0: s.lastIndexOf("0"), michael@0: ["02134"]); michael@0: michael@0: re.lastIndex = s.lastIndexOf("0") + 1; michael@0: michael@0: AddRegExpCases( re, michael@0: "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + michael@0: s.lastIndexOf("0") +1, michael@0: s, michael@0: 0, michael@0: null); michael@0: */ michael@0: test(); michael@0: michael@0: function AddRegExpCases( michael@0: regexp, str_regexp, string, str_string, index, matches_array ) { michael@0: michael@0: // prevent a runtime error michael@0: michael@0: if ( regexp.exec(string) == null || matches_array == null ) { michael@0: AddTestCase( michael@0: string + ".match(" + 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: 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: str_string, michael@0: string.match(regexp).input ); 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: } 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 ( regexp.exec(string) == null || matches_array == null ) { michael@0: AddTestCase( michael@0: regexp + ".exec(" + string +")", michael@0: matches_array, michael@0: regexp.exec(string) ); 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: }