| 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-004.js |
| 9 |
* ECMA Section: 15.6.4.9 |
| 10 |
* Description: Based on ECMA 2 Draft 7 February 1999 |
| 11 |
* |
| 12 |
* Author: [email protected] |
| 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 |
* The match function should be intentionally generic, and not require |
| 39 |
* this to be a string. |
| 40 |
* |
| 41 |
*/ |
| 42 |
|
| 43 |
var SECTION = "String/match-004.js"; |
| 44 |
var TITLE = "String.prototype.match( regexp )"; |
| 45 |
|
| 46 |
var BUGNUMBER="http://scopus/bugsplat/show_bug.cgi?id=345818"; |
| 47 |
|
| 48 |
printBugNumber(BUGNUMBER); |
| 49 |
|
| 50 |
// set the value of lastIndex |
| 51 |
re = /0./; |
| 52 |
s = 10203040506070809000; |
| 53 |
|
| 54 |
Number.prototype.match = String.prototype.match; |
| 55 |
|
| 56 |
AddRegExpCases( re, |
| 57 |
"re = " + re , |
| 58 |
s, |
| 59 |
String(s), |
| 60 |
1, |
| 61 |
["02"]); |
| 62 |
|
| 63 |
|
| 64 |
re.lastIndex = 0; |
| 65 |
AddRegExpCases( re, |
| 66 |
"re = " + re +" [lastIndex is " + re.lastIndex+"]", |
| 67 |
s, |
| 68 |
String(s), |
| 69 |
1, |
| 70 |
["02"]); |
| 71 |
/* |
| 72 |
|
| 73 |
re.lastIndex = s.length; |
| 74 |
|
| 75 |
AddRegExpCases( re, |
| 76 |
"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
| 77 |
s.length, |
| 78 |
s, |
| 79 |
s.lastIndexOf("0"), |
| 80 |
null ); |
| 81 |
|
| 82 |
re.lastIndex = s.lastIndexOf("0"); |
| 83 |
|
| 84 |
AddRegExpCases( re, |
| 85 |
"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
| 86 |
s.lastIndexOf("0"), |
| 87 |
s, |
| 88 |
s.lastIndexOf("0"), |
| 89 |
["02134"]); |
| 90 |
|
| 91 |
re.lastIndex = s.lastIndexOf("0") + 1; |
| 92 |
|
| 93 |
AddRegExpCases( re, |
| 94 |
"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
| 95 |
s.lastIndexOf("0") +1, |
| 96 |
s, |
| 97 |
0, |
| 98 |
null); |
| 99 |
*/ |
| 100 |
test(); |
| 101 |
|
| 102 |
function AddRegExpCases( |
| 103 |
regexp, str_regexp, string, str_string, index, matches_array ) { |
| 104 |
|
| 105 |
// prevent a runtime error |
| 106 |
|
| 107 |
if ( regexp.exec(string) == null || matches_array == null ) { |
| 108 |
AddTestCase( |
| 109 |
string + ".match(" + regexp +")", |
| 110 |
matches_array, |
| 111 |
string.match(regexp) ); |
| 112 |
|
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
AddTestCase( |
| 117 |
"( " + string + " ).match(" + str_regexp +").length", |
| 118 |
matches_array.length, |
| 119 |
string.match(regexp).length ); |
| 120 |
|
| 121 |
AddTestCase( |
| 122 |
"( " + string + " ).match(" + str_regexp +").index", |
| 123 |
index, |
| 124 |
string.match(regexp).index ); |
| 125 |
|
| 126 |
AddTestCase( |
| 127 |
"( " + string + " ).match(" + str_regexp +").input", |
| 128 |
str_string, |
| 129 |
string.match(regexp).input ); |
| 130 |
|
| 131 |
var limit = matches_array.length > string.match(regexp).length ? |
| 132 |
matches_array.length : |
| 133 |
string.match(regexp).length; |
| 134 |
|
| 135 |
for ( var matches = 0; matches < limit; matches++ ) { |
| 136 |
AddTestCase( |
| 137 |
"( " + string + " ).match(" + str_regexp +")[" + matches +"]", |
| 138 |
matches_array[matches], |
| 139 |
string.match(regexp)[matches] ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
function AddGlobalRegExpCases( |
| 144 |
regexp, str_regexp, string, matches_array ) { |
| 145 |
|
| 146 |
// prevent a runtime error |
| 147 |
|
| 148 |
if ( regexp.exec(string) == null || matches_array == null ) { |
| 149 |
AddTestCase( |
| 150 |
regexp + ".exec(" + string +")", |
| 151 |
matches_array, |
| 152 |
regexp.exec(string) ); |
| 153 |
|
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
AddTestCase( |
| 158 |
"( " + string + " ).match(" + str_regexp +").length", |
| 159 |
matches_array.length, |
| 160 |
string.match(regexp).length ); |
| 161 |
|
| 162 |
var limit = matches_array.length > string.match(regexp).length ? |
| 163 |
matches_array.length : |
| 164 |
string.match(regexp).length; |
| 165 |
|
| 166 |
for ( var matches = 0; matches < limit; matches++ ) { |
| 167 |
AddTestCase( |
| 168 |
"( " + string + " ).match(" + str_regexp +")[" + matches +"]", |
| 169 |
matches_array[matches], |
| 170 |
string.match(regexp)[matches] ); |
| 171 |
} |
| 172 |
} |