Debian Codegraph @

rhino/testsrc/tests/ecma_2/String/match-003.js

raw symbols SCIP
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 gTestfile = 'match-003.js';
7
8 /**
9 * File Name: String/match-003.js
10 * ECMA Section: 15.6.4.9
11 * Description: Based on ECMA 2 Draft 7 February 1999
12 *
13 * Author: [email protected]
14 * Date: 19 February 1999
15 */
16
17 /*
18 * String.match( regexp )
19 *
20 * If regexp is not an object of type RegExp, it is replaced with result
21 * of the expression new RegExp(regexp). Let string denote the result of
22 * converting the this value to a string. If regexp.global is false,
23 * return the result obtained by invoking RegExp.prototype.exec (see
24 * section 15.7.5.3) on regexp with string as parameter.
25 *
26 * Otherwise, set the regexp.lastIndex property to 0 and invoke
27 * RegExp.prototype.exec repeatedly until there is no match. If there is a
28 * match with an empty string (in other words, if the value of
29 * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
30 * The value returned is an array with the properties 0 through n-1
31 * corresponding to the first element of the result of each matching
32 * invocation of RegExp.prototype.exec.
33 *
34 * Note that the match function is intentionally generic; it does not
35 * require that its this value be a string object. Therefore, it can be
36 * transferred to other kinds of objects for use as a method.
37 */
38
39 var SECTION = "String/match-003.js";
40 var VERSION = "ECMA_2";
41 var TITLE = "String.prototype.match( regexp )";
42
43 startTest();
44
45 // the regexp argument is not a RegExp object
46 // this is not a string object
47
48
49 // [if regexp.global is true] set the regexp.lastIndex property to 0 and
50 // invoke RegExp.prototype.exec repeatedly until there is no match. If
51 // there is a match with an empty string (in other words, if the value of
52 // regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
53 // The value returned is an array with the properties 0 through n-1
54 // corresponding to the first element of the result of each matching invocation
55 // of RegExp.prototype.exec.
56
57
58 // set the value of lastIndex
59 re = /([\d]{5})([-\ ]?[\d]{4})?$/g;
60
61
62 s = "Boston, MA 02134";
63
64 AddGlobalRegExpCases( re,
65 "re = " + re,
66 s,
67 ["02134" ]);
68
69 re.lastIndex = 0;
70
71 AddGlobalRegExpCases(
72 re,
73 "re = " + re + "; re.lastIndex = 0 ",
74 s,
75 ["02134"]);
76
77
78 re.lastIndex = s.length;
79
80 AddGlobalRegExpCases(
81 re,
82 "re = " + re + "; re.lastIndex = " + s.length,
83 s,
84 ["02134"] );
85
86 re.lastIndex = s.lastIndexOf("0");
87
88 AddGlobalRegExpCases(
89 re,
90 "re = "+ re +"; re.lastIndex = " + s.lastIndexOf("0"),
91 s,
92 ["02134"]);
93
94 re.lastIndex = s.lastIndexOf("0") + 1;
95
96 AddGlobalRegExpCases(
97 re,
98 "re = " +re+ "; re.lastIndex = " + (s.lastIndexOf("0") +1),
99 s,
100 ["02134"]);
101
102 test();
103
104 function AddGlobalRegExpCases(
105 regexp, str_regexp, string, matches_array ) {
106
107 // prevent a runtime error
108
109 if ( string.match(regexp) == null || matches_array == null ) {
110 AddTestCase(
111 string + ".match(" + str_regexp +")",
112 matches_array,
113 string.match(regexp) );
114
115 return;
116 }
117
118 AddTestCase(
119 "( " + string + " ).match(" + str_regexp +").length",
120 matches_array.length,
121 string.match(regexp).length );
122
123 var limit = matches_array.length > string.match(regexp).length ?
124 matches_array.length :
125 string.match(regexp).length;
126
127 for ( var matches = 0; matches < limit; matches++ ) {
128 AddTestCase(
129 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
130 matches_array[matches],
131 string.match(regexp)[matches] );
132 }
133 }