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): mozilla@pdavis.cx, pschwartau@netscape.com 20* Date: 22 October 2001 21* 22* SUMMARY: Regression test for Bugzilla bug 105972: 23* "/^.*?$/ will not match anything" 24* 25* See http://bugzilla.mozilla.org/show_bug.cgi?id=105972 26*/ 27//----------------------------------------------------------------------------- 28var i = 0; 29var bug = 105972; 30var summary = 'Regression test for Bugzilla bug 105972'; 31var cnEmptyString = ''; 32var status = ''; 33var statusmessages = new Array(); 34var pattern = ''; 35var patterns = new Array(); 36var string = ''; 37var strings = new Array(); 38var actualmatch = ''; 39var actualmatches = new Array(); 40var expectedmatch = ''; 41var expectedmatches = new Array(); 42 43 44/* 45 * The bug: this match was coming up null in Rhino and SpiderMonkey. 46 * It should match the whole string. The reason: 47 * 48 * The * operator is greedy, but *? is non-greedy: it will stop 49 * at the simplest match it can find. But the pattern here asks us 50 * to match till the end of the string. So the simplest match must 51 * go all the way out to the end, and *? has no choice but to do it. 52 */ 53status = inSection(1); 54pattern = /^.*?$/; 55string = 'Hello World'; 56actualmatch = string.match(pattern); 57expectedmatch = Array(string); 58addThis(); 59 60 61/* 62 * Leave off the '$' condition - here we expect the empty string. 63 * Unlike the above pattern, we don't have to match till the end of 64 * the string, so the non-greedy operator *? doesn't try to... 65 */ 66status = inSection(2); 67pattern = /^.*?/; 68string = 'Hello World'; 69actualmatch = string.match(pattern); 70expectedmatch = Array(cnEmptyString); 71addThis(); 72 73 74/* 75 * Try '$' combined with an 'or' operator. 76 * 77 * The operator *? will consume the string from left to right, 78 * attempting to satisfy the condition (:|$). When it hits ':', 79 * the match will stop because the operator *? is non-greedy. 80 * 81 * The submatch $1 = (:|$) will contain the ':' 82 */ 83status = inSection(3); 84pattern = /^.*?(:|$)/; 85string = 'Hello: World'; 86actualmatch = string.match(pattern); 87expectedmatch = Array('Hello:', ':'); 88addThis(); 89 90 91/* 92 * Again, '$' combined with an 'or' operator. 93 * 94 * The operator * will consume the string from left to right, 95 * attempting to satisfy the condition (:|$). When it hits ':', 96 * the match will not stop since * is greedy. The match will 97 * continue until it hits $, the end-of-string boundary. 98 * 99 * The submatch $1 = (:|$) will contain the empty string 100 * conceived to exist at the end-of-string boundary. 101 */ 102status = inSection(4); 103pattern = /^.*(:|$)/; 104string = 'Hello: World'; 105actualmatch = string.match(pattern); 106expectedmatch = Array(string, cnEmptyString); 107addThis(); 108 109 110 111 112//----------------------------------------------------------------------------- 113test(); 114//----------------------------------------------------------------------------- 115 116 117 118function addThis() 119{ 120 statusmessages[i] = status; 121 patterns[i] = pattern; 122 strings[i] = string; 123 actualmatches[i] = actualmatch; 124 expectedmatches[i] = expectedmatch; 125 i++; 126} 127 128 129function test() 130{ 131 enterFunc ('test'); 132 printBugNumber (bug); 133 printStatus (summary); 134 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); 135 exitFunc ('test'); 136} 137