js/src/tests/ecma_2/String/match-003.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:54380259aa87
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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-003.js
9 * ECMA Section: 15.6.4.9
10 * Description: Based on ECMA 2 Draft 7 February 1999
11 *
12 * Author: christine@netscape.com
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 var SECTION = "String/match-003.js";
39 var VERSION = "ECMA_2";
40 var TITLE = "String.prototype.match( regexp )";
41
42 startTest();
43
44 // the regexp argument is not a RegExp object
45 // this is not a string object
46
47
48 // [if regexp.global is true] set the regexp.lastIndex property to 0 and
49 // invoke RegExp.prototype.exec repeatedly until there is no match. If
50 // there is a match with an empty string (in other words, if the value of
51 // regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
52 // The value returned is an array with the properties 0 through n-1
53 // corresponding to the first element of the result of each matching invocation
54 // of RegExp.prototype.exec.
55
56
57 // set the value of lastIndex
58 re = /([\d]{5})([-\ ]?[\d]{4})?$/g;
59
60
61 s = "Boston, MA 02134";
62
63 AddGlobalRegExpCases( re,
64 "re = " + re,
65 s,
66 ["02134" ]);
67
68 re.lastIndex = 0;
69
70 AddGlobalRegExpCases(
71 re,
72 "re = " + re + "; re.lastIndex = 0 ",
73 s,
74 ["02134"]);
75
76
77 re.lastIndex = s.length;
78
79 AddGlobalRegExpCases(
80 re,
81 "re = " + re + "; re.lastIndex = " + s.length,
82 s,
83 ["02134"] );
84
85 re.lastIndex = s.lastIndexOf("0");
86
87 AddGlobalRegExpCases(
88 re,
89 "re = "+ re +"; re.lastIndex = " + s.lastIndexOf("0"),
90 s,
91 ["02134"]);
92
93 re.lastIndex = s.lastIndexOf("0") + 1;
94
95 AddGlobalRegExpCases(
96 re,
97 "re = " +re+ "; re.lastIndex = " + (s.lastIndexOf("0") +1),
98 s,
99 ["02134"]);
100
101 test();
102
103 function AddGlobalRegExpCases(
104 regexp, str_regexp, string, matches_array ) {
105
106 // prevent a runtime error
107
108 if ( string.match(regexp) == null || matches_array == null ) {
109 AddTestCase(
110 string + ".match(" + str_regexp +")",
111 matches_array,
112 string.match(regexp) );
113
114 return;
115 }
116
117 AddTestCase(
118 "( " + string + " ).match(" + str_regexp +").length",
119 matches_array.length,
120 string.match(regexp).length );
121
122 var limit = matches_array.length > string.match(regexp).length ?
123 matches_array.length :
124 string.match(regexp).length;
125
126 for ( var matches = 0; matches < limit; matches++ ) {
127 AddTestCase(
128 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
129 matches_array[matches],
130 string.match(regexp)[matches] );
131 }
132 }

mercurial