1// Copyright 2014 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Flags: --harmony-regexps 29 30var re = /foo.bar/; 31 32assertTrue(!!"foo*bar".match(re)); 33assertTrue(!!"..foo*bar".match(re)); 34 35var plain = /foobar/; 36 37assertTrue(!!"foobar".match(plain)); 38assertTrue(!!"..foobar".match(plain)); 39 40var sticky = /foo.bar/y; 41 42assertTrue(!!"foo*bar".match(sticky)); 43assertEquals(0, sticky.lastIndex); 44assertFalse(!!"..foo*bar".match(sticky)); 45 46var stickyplain = /foobar/y; 47 48assertTrue(!!"foobar".match(stickyplain)); 49assertEquals(0, stickyplain.lastIndex); 50assertFalse(!!"..foobar".match(stickyplain)); 51 52var global = /foo.bar/g; 53 54assertTrue(global.test("foo*bar")); 55assertFalse(global.test("..foo*bar")); 56global.lastIndex = 0; 57assertTrue(global.test("..foo*bar")); 58 59var plainglobal = /foobar/g; 60 61assertTrue(plainglobal.test("foobar")); 62assertFalse(plainglobal.test("foobar")); 63plainglobal.lastIndex = 0; 64assertTrue(plainglobal.test("foobar")); 65 66var stickyglobal = /foo.bar/gy; 67 68assertTrue(stickyglobal.test("foo*bar")); 69assertEquals(7, stickyglobal.lastIndex); 70assertFalse(stickyglobal.test("..foo*bar")); 71stickyglobal.lastIndex = 0; 72assertFalse(stickyglobal.test("..foo*bar")); 73stickyglobal.lastIndex = 2; 74assertTrue(stickyglobal.test("..foo*bar")); 75assertEquals(9, stickyglobal.lastIndex); 76 77var stickyplainglobal = /foobar/yg; 78assertTrue(stickyplainglobal.sticky); 79stickyplainglobal.sticky = false; 80 81assertTrue(stickyplainglobal.test("foobar")); 82assertEquals(6, stickyplainglobal.lastIndex); 83assertFalse(stickyplainglobal.test("..foobar")); 84stickyplainglobal.lastIndex = 0; 85assertFalse(stickyplainglobal.test("..foobar")); 86stickyplainglobal.lastIndex = 2; 87assertTrue(stickyplainglobal.test("..foobar")); 88assertEquals(8, stickyplainglobal.lastIndex); 89 90assertEquals("/foo.bar/gy", "" + stickyglobal); 91assertEquals("/foo.bar/g", "" + global); 92 93assertTrue(stickyglobal.sticky); 94stickyglobal.sticky = false; 95assertTrue(stickyglobal.sticky); 96 97var stickyglobal2 = new RegExp("foo.bar", "gy"); 98assertTrue(stickyglobal2.test("foo*bar")); 99assertEquals(7, stickyglobal2.lastIndex); 100assertFalse(stickyglobal2.test("..foo*bar")); 101stickyglobal2.lastIndex = 0; 102assertFalse(stickyglobal2.test("..foo*bar")); 103stickyglobal2.lastIndex = 2; 104assertTrue(stickyglobal2.test("..foo*bar")); 105assertEquals(9, stickyglobal2.lastIndex); 106 107assertEquals("/foo.bar/gy", "" + stickyglobal2); 108 109assertTrue(stickyglobal2.sticky); 110stickyglobal2.sticky = false; 111assertTrue(stickyglobal2.sticky); 112 113sticky.lastIndex = -1; // Causes sticky regexp to fail fast 114assertFalse(sticky.test("..foo.bar")); 115assertEquals(0, sticky.lastIndex); 116 117sticky.lastIndex = -1; // Causes sticky regexp to fail fast 118assertFalse(!!sticky.exec("..foo.bar")); 119assertEquals(0, sticky.lastIndex); 120 121// ES6 draft says: Even when the y flag is used with a pattern, ^ always 122// matches only at the beginning of Input, or (if Multiline is true) at the 123// beginning of a line. 124var hat = /^foo/y; 125hat.lastIndex = 2; 126assertFalse(hat.test("..foo")); 127 128var mhat = /^foo/my; 129mhat.lastIndex = 2; 130assertFalse(mhat.test("..foo")); 131mhat.lastIndex = 2; 132assertTrue(mhat.test(".\nfoo")); 133