Debian Codegraph @

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

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