]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_3/RegExp/15.10.6.2-1.js
2 * The contents of this file are subject to the Netscape Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/NPL/
7 * Software distributed under the License is distributed on an "AS IS"
8 * basis, WITHOUT WARRANTY OF ANY KIND, either expressed
9 * or implied. See the License for the specific language governing
10 * rights and limitations under the License.
12 * The Original Code is mozilla.org code.
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation.
17 * All Rights Reserved.
19 * Contributor(s): pschwartau@netscape.com
20 * Date: 23 October 2001
22 * SUMMARY: Testing regexps with the global flag set.
23 * NOT every substring fitting the given pattern will be matched.
24 * The parent string is CONSUMED as successive matches are found.
26 * From the ECMA-262 Final spec:
28 * 15.10.6.2 RegExp.prototype.exec(string)
29 * Performs a regular expression match of string against the regular
30 * expression and returns an Array object containing the results of
31 * the match, or null if the string did not match.
33 * The string ToString(string) is searched for an occurrence of the
34 * regular expression pattern as follows:
36 * 1. Let S be the value of ToString(string).
37 * 2. Let length be the length of S.
38 * 3. Let lastIndex be the value of the lastIndex property.
39 * 4. Let i be the value of ToInteger(lastIndex).
40 * 5. If the global property is false, let i = 0.
41 * 6. If i < 0 or i > length then set lastIndex to 0 and return null.
42 * 7. Call [[Match]], giving it the arguments S and i.
43 * If [[Match]] returned failure, go to step 8;
44 * otherwise let r be its State result and go to step 10.
47 * 10. Let e be r's endIndex value.
48 * 11. If the global property is true, set lastIndex to e.
53 * So when the global flag is set, |lastIndex| is incremented every time
54 * there is a match; not from i to i+1, but from i to "endIndex" e:
56 * e = (index of last input character matched so far by the pattern) + 1
58 * Thus in the example below, the first endIndex e occurs after the
59 * first match 'a b'. The next match will begin AFTER this, and so
60 * will NOT be 'b c', but rather 'c d'. Similarly, 'd e' won't be matched.
62 //-----------------------------------------------------------------------------
65 var summary
= 'Testing regexps with the global flag set';
67 var statusmessages
= new Array();
69 var patterns
= new Array();
71 var strings
= new Array();
73 var actualmatches
= new Array();
74 var expectedmatch
= '';
75 var expectedmatches
= new Array();
78 status
= inSection(1);
81 actualmatch
= string
.match(pattern
);
82 expectedmatch
= ['a b','c d']; // see above explanation -
86 status
= inSection(2);
89 actualmatch
= string
.match(pattern
);
90 expectedmatch
= ['123','456'];
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
103 statusmessages
[i
] = status
;
104 patterns
[i
] = pattern
;
106 actualmatches
[i
] = actualmatch
;
107 expectedmatches
[i
] = expectedmatch
;
115 printBugNumber (bug
);
116 printStatus (summary
);
117 testRegExp(statusmessages
, patterns
, strings
, actualmatches
, expectedmatches
);