1/* 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/ 6* 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. 11* 12* The Original Code is mozilla.org code. 13* 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. 18* 19* Contributor(s): pschwartau@netscape.com 20* Date: 23 October 2001 21* 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. 25* 26* From the ECMA-262 Final spec: 27* 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. 32* 33* The string ToString(string) is searched for an occurrence of the 34* regular expression pattern as follows: 35* 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. 45* 8. Let i = i+1. 46* 9. Go to step 6. 47* 10. Let e be r's endIndex value. 48* 11. If the global property is true, set lastIndex to e. 49* 50* etc. 51* 52* 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: 55* 56* e = (index of last input character matched so far by the pattern) + 1 57* 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. 61*/ 62//----------------------------------------------------------------------------- 63var i = 0; 64var bug = '(none)'; 65var summary = 'Testing regexps with the global flag set'; 66var status = ''; 67var statusmessages = new Array(); 68var pattern = ''; 69var patterns = new Array(); 70var string = ''; 71var strings = new Array(); 72var actualmatch = ''; 73var actualmatches = new Array(); 74var expectedmatch = ''; 75var expectedmatches = new Array(); 76 77 78status = inSection(1); 79string = 'a b c d e'; 80pattern = /\w\s\w/g; 81actualmatch = string.match(pattern); 82expectedmatch = ['a b','c d']; // see above explanation - 83addThis(); 84 85 86status = inSection(2); 87string = '12345678'; 88pattern = /\d\d\d/g; 89actualmatch = string.match(pattern); 90expectedmatch = ['123','456']; 91addThis(); 92 93 94 95//----------------------------------------------------------------------------- 96test(); 97//----------------------------------------------------------------------------- 98 99 100 101function addThis() 102{ 103 statusmessages[i] = status; 104 patterns[i] = pattern; 105 strings[i] = string; 106 actualmatches[i] = actualmatch; 107 expectedmatches[i] = expectedmatch; 108 i++; 109} 110 111 112function test() 113{ 114 enterFunc ('test'); 115 printBugNumber (bug); 116 printStatus (summary); 117 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); 118 exitFunc ('test'); 119} 120