1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * pschwartau@netscape.com
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 * Date: 23 October 2001
42 * SUMMARY: Testing regexps with the global flag set.
43 * NOT every substring fitting the given pattern will be matched.
44 * The parent string is CONSUMED as successive matches are found.
46 * From the ECMA-262 Final spec:
48 * 15.10.6.2 RegExp.prototype.exec(string)
49 * Performs a regular expression match of string against the regular
50 * expression and returns an Array object containing the results of
51 * the match, or null if the string did not match.
53 * The string ToString(string) is searched for an occurrence of the
54 * regular expression pattern as follows:
56 * 1. Let S be the value of ToString(string).
57 * 2. Let length be the length of S.
58 * 3. Let lastIndex be the value of the lastIndex property.
59 * 4. Let i be the value of ToInteger(lastIndex).
60 * 5. If the global property is false, let i = 0.
61 * 6. If i < 0 or i > length then set lastIndex to 0 and return null.
62 * 7. Call [[Match]], giving it the arguments S and i.
63 * If [[Match]] returned failure, go to step 8;
64 * otherwise let r be its State result and go to step 10.
67 * 10. Let e be r's endIndex value.
68 * 11. If the global property is true, set lastIndex to e.
73 * So when the global flag is set, |lastIndex| is incremented every time
74 * there is a match; not from i to i+1, but from i to "endIndex" e:
76 * e = (index of last input character matched so far by the pattern) + 1
78 * Thus in the example below, the first endIndex e occurs after the
79 * first match 'a b'. The next match will begin AFTER this, and so
80 * will NOT be 'b c', but rather 'c d'. Similarly, 'd e' won't be matched.
82 //-----------------------------------------------------------------------------
84 var BUGNUMBER = '(none)';
85 var summary = 'Testing regexps with the global flag set';
87 var statusmessages = new Array();
89 var patterns = new Array();
91 var strings = new Array();
93 var actualmatches = new Array();
94 var expectedmatch = '';
95 var expectedmatches = new Array();
98 status = inSection(1);
101 actualmatch = string.match(pattern);
102 expectedmatch = ['a b','c d']; // see above explanation -
106 status = inSection(2);
109 actualmatch = string.match(pattern);
110 expectedmatch = ['123','456'];
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
123 statusmessages[i] = status;
124 patterns[i] = pattern;
126 actualmatches[i] = actualmatch;
127 expectedmatches[i] = expectedmatch;
135 printBugNumber(BUGNUMBER);
136 printStatus (summary);
137 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);