| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
gTestfile = 'match-002.js'; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
var SECTION = "String/match-002.js"; |
| 44 |
var VERSION = "ECMA_2"; |
| 45 |
var TITLE = "String.prototype.match( regexp )"; |
| 46 |
|
| 47 |
startTest(); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
AddRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/, |
| 53 |
"/([\d]{5})([-\ ]?[\d]{4})?$/", |
| 54 |
"Boston, Mass. 02134", |
| 55 |
14, |
| 56 |
["02134", "02134", undefined]); |
| 57 |
|
| 58 |
AddGlobalRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/g, |
| 59 |
"/([\d]{5})([-\ ]?[\d]{4})?$/g", |
| 60 |
"Boston, Mass. 02134", |
| 61 |
["02134"]); |
| 62 |
|
| 63 |
|
| 64 |
re = /([\d]{5})([-\ ]?[\d]{4})?$/; |
| 65 |
re.lastIndex = 0; |
| 66 |
|
| 67 |
s = "Boston, MA 02134"; |
| 68 |
|
| 69 |
AddRegExpCases( re, |
| 70 |
"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex =0", |
| 71 |
s, |
| 72 |
s.lastIndexOf("0"), |
| 73 |
["02134", "02134", undefined]); |
| 74 |
|
| 75 |
|
| 76 |
re.lastIndex = s.length; |
| 77 |
|
| 78 |
AddRegExpCases( re, |
| 79 |
"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
| 80 |
s.length, |
| 81 |
s, |
| 82 |
s.lastIndexOf("0"), |
| 83 |
["02134", "02134", undefined] ); |
| 84 |
|
| 85 |
re.lastIndex = s.lastIndexOf("0"); |
| 86 |
|
| 87 |
AddRegExpCases( re, |
| 88 |
"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
| 89 |
s.lastIndexOf("0"), |
| 90 |
s, |
| 91 |
s.lastIndexOf("0"), |
| 92 |
["02134", "02134", undefined]); |
| 93 |
|
| 94 |
re.lastIndex = s.lastIndexOf("0") + 1; |
| 95 |
|
| 96 |
AddRegExpCases( re, |
| 97 |
"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + |
| 98 |
s.lastIndexOf("0") +1, |
| 99 |
s, |
| 100 |
s.lastIndexOf("0"), |
| 101 |
["02134", "02134", undefined]); |
| 102 |
|
| 103 |
test(); |
| 104 |
|
| 105 |
function AddRegExpCases( |
| 106 |
regexp, str_regexp, string, index, matches_array ) { |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if ( regexp.exec(string) == null || matches_array == null ) { |
| 111 |
AddTestCase( |
| 112 |
string + ".match(" + regexp +")", |
| 113 |
matches_array, |
| 114 |
string.match(regexp) ); |
| 115 |
|
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
AddTestCase( |
| 120 |
"( " + string + " ).match(" + str_regexp +").length", |
| 121 |
matches_array.length, |
| 122 |
string.match(regexp).length ); |
| 123 |
|
| 124 |
AddTestCase( |
| 125 |
"( " + string + " ).match(" + str_regexp +").index", |
| 126 |
index, |
| 127 |
string.match(regexp).index ); |
| 128 |
|
| 129 |
AddTestCase( |
| 130 |
"( " + string + " ).match(" + str_regexp +").input", |
| 131 |
string, |
| 132 |
string.match(regexp).input ); |
| 133 |
|
| 134 |
var limit = matches_array.length > string.match(regexp).length ? |
| 135 |
matches_array.length : |
| 136 |
string.match(regexp).length; |
| 137 |
|
| 138 |
for ( var matches = 0; matches < limit; matches++ ) { |
| 139 |
AddTestCase( |
| 140 |
"( " + string + " ).match(" + str_regexp +")[" + matches +"]", |
| 141 |
matches_array[matches], |
| 142 |
string.match(regexp)[matches] ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
function AddGlobalRegExpCases( |
| 147 |
regexp, str_regexp, string, matches_array ) { |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
if ( regexp.exec(string) == null || matches_array == null ) { |
| 152 |
AddTestCase( |
| 153 |
regexp + ".exec(" + string +")", |
| 154 |
matches_array, |
| 155 |
regexp.exec(string) ); |
| 156 |
|
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
AddTestCase( |
| 161 |
"( " + string + " ).match(" + str_regexp +").length", |
| 162 |
matches_array.length, |
| 163 |
string.match(regexp).length ); |
| 164 |
|
| 165 |
var limit = matches_array.length > string.match(regexp).length ? |
| 166 |
matches_array.length : |
| 167 |
string.match(regexp).length; |
| 168 |
|
| 169 |
for ( var matches = 0; matches < limit; matches++ ) { |
| 170 |
AddTestCase( |
| 171 |
"( " + string + " ).match(" + str_regexp +")[" + matches +"]", |
| 172 |
matches_array[matches], |
| 173 |
string.match(regexp)[matches] ); |
| 174 |
} |
| 175 |
} |