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 30 var re = /foo.bar/; 31 32 assertTrue(!!"foo*bar".match(re)); 33 assertTrue(!!"..foo*bar".match(re)); 34 35 var plain = /foobar/; 36 37 assertTrue(!!"foobar".match(plain)); 38 assertTrue(!!"..foobar".match(plain)); 39 40 var sticky = /foo.bar/y; 41 42 assertTrue(!!"foo*bar".match(sticky)); 43 assertEquals(0, sticky.lastIndex); 44 assertFalse(!!"..foo*bar".match(sticky)); 45 46 var stickyplain = /foobar/y; 47 48 assertTrue(!!"foobar".match(stickyplain)); 49 assertEquals(0, stickyplain.lastIndex); 50 assertFalse(!!"..foobar".match(stickyplain)); 51 52 var global = /foo.bar/g; 53 54 assertTrue(global.test("foo*bar")); 55 assertFalse(global.test("..foo*bar")); 56 global.lastIndex = 0; 57 assertTrue(global.test("..foo*bar")); 58 59 var plainglobal = /foobar/g; 60 61 assertTrue(plainglobal.test("foobar")); 62 assertFalse(plainglobal.test("foobar")); 63 plainglobal.lastIndex = 0; 64 assertTrue(plainglobal.test("foobar")); 65 66 var stickyglobal = /foo.bar/gy; 67 68 assertTrue(stickyglobal.test("foo*bar")); 69 assertEquals(7, stickyglobal.lastIndex); 70 assertFalse(stickyglobal.test("..foo*bar")); 71 stickyglobal.lastIndex = 0; 72 assertFalse(stickyglobal.test("..foo*bar")); 73 stickyglobal.lastIndex = 2; 74 assertTrue(stickyglobal.test("..foo*bar")); 75 assertEquals(9, stickyglobal.lastIndex); 76 77 var stickyplainglobal = /foobar/yg; 78 assertTrue(stickyplainglobal.sticky); 79 stickyplainglobal.sticky = false; 80 81 assertTrue(stickyplainglobal.test("foobar")); 82 assertEquals(6, stickyplainglobal.lastIndex); 83 assertFalse(stickyplainglobal.test("..foobar")); 84 stickyplainglobal.lastIndex = 0; 85 assertFalse(stickyplainglobal.test("..foobar")); 86 stickyplainglobal.lastIndex = 2; 87 assertTrue(stickyplainglobal.test("..foobar")); 88 assertEquals(8, stickyplainglobal.lastIndex); 89 90 assertEquals("/foo.bar/gy", "" + stickyglobal); 91 assertEquals("/foo.bar/g", "" + global); 92 93 assertTrue(stickyglobal.sticky); 94 stickyglobal.sticky = false; 95 assertTrue(stickyglobal.sticky); 96 97 var stickyglobal2 = new RegExp("foo.bar", "gy"); 98 assertTrue(stickyglobal2.test("foo*bar")); 99 assertEquals(7, stickyglobal2.lastIndex); 100 assertFalse(stickyglobal2.test("..foo*bar")); 101 stickyglobal2.lastIndex = 0; 102 assertFalse(stickyglobal2.test("..foo*bar")); 103 stickyglobal2.lastIndex = 2; 104 assertTrue(stickyglobal2.test("..foo*bar")); 105 assertEquals(9, stickyglobal2.lastIndex); 106 107 assertEquals("/foo.bar/gy", "" + stickyglobal2); 108 109 assertTrue(stickyglobal2.sticky); 110 stickyglobal2.sticky = false; 111 assertTrue(stickyglobal2.sticky); 112 113 sticky.lastIndex = -1; // Causes sticky regexp to fail fast 114 assertFalse(sticky.test("..foo.bar")); 115 assertEquals(0, sticky.lastIndex); 116 117 sticky.lastIndex = -1; // Causes sticky regexp to fail fast 118 assertFalse(!!sticky.exec("..foo.bar")); 119 assertEquals(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. 124 var hat = /^foo/y; 125 hat.lastIndex = 2; 126 assertFalse(hat.test("..foo")); 127 128 var mhat = /^foo/my; 129 mhat.lastIndex = 2; 130 assertFalse(mhat.test("..foo")); 131 mhat.lastIndex = 2; 132 assertTrue(mhat.test(".\nfoo")); 133