1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 description("This page tests handling of parentheses subexpressions."); 25 26 var regexp1 = /(a|A)(b|B)/; 27 shouldBe("regexp1.exec('abc')", "['ab','a','b']"); 28 29 var regexp2 = /(a((b)|c|d))e/; 30 shouldBe("regexp2.exec('abacadabe')", "['abe','ab','b','b']"); 31 32 var regexp3 = /(a(b|(c)|d))e/; 33 shouldBe("regexp3.exec('abacadabe')", "['abe','ab','b',undefined]"); 34 35 var regexp4 = /(a(b|c|(d)))e/; 36 shouldBe("regexp4.exec('abacadabe')", "['abe','ab','b',undefined]"); 37 38 var regexp5 = /(a((b)|(c)|(d)))e/; 39 shouldBe("regexp5.exec('abacadabe')", "['abe','ab','b','b',undefined,undefined]"); 40 41 var regexp6 = /(a((b)|(c)|(d)))/; 42 shouldBe("regexp6.exec('abcde')", "['ab','ab','b','b',undefined,undefined]"); 43 44 var regexp7 = /(a(b)??)??c/; 45 shouldBe("regexp7.exec('abc')", "['abc','ab','b']"); 46 47 var regexp8 = /(a|(e|q))(x|y)/; 48 shouldBe("regexp8.exec('bcaddxqy')" , "['qy','q','q','y']"); 49 50 var regexp9 = /((t|b)?|a)$/; 51 shouldBe("regexp9.exec('asdfjejgsdflaksdfjkeljghkjea')", "['a','a',undefined]"); 52 53 var regexp10 = /(?:h|e?(?:t|b)?|a?(?:t|b)?)(?:$)/; 54 shouldBe("regexp10.exec('asdfjejgsdflaksdfjkeljghat')", "['at']"); 55 56 var regexp11 = /([Jj]ava([Ss]cript)?)\sis\s(fun\w*)/; 57 shouldBeNull("regexp11.exec('Developing with JavaScript is dangerous, do not try it without assistance')"); 58 59 var regexp12 = /(?:(.+), )?(.+), (..) to (?:(.+), )?(.+), (..)/; 60 shouldBe("regexp12.exec('Seattle, WA to Buckley, WA')", "['Seattle, WA to Buckley, WA', undefined, 'Seattle', 'WA', undefined, 'Buckley', 'WA']"); 61 62 var regexp13 = /(A)?(A.*)/; 63 shouldBe("regexp13.exec('zxcasd;fl\ ^AaaAAaaaf;lrlrzs')", "['AaaAAaaaf;lrlrzs',undefined,'AaaAAaaaf;lrlrzs']"); 64 65 var regexp14 = /(a)|(b)/; 66 shouldBe("regexp14.exec('b')", "['b',undefined,'b']"); 67 68 var regexp15 = /^(?!(ab)de|x)(abd)(f)/; 69 shouldBe("regexp15.exec('abdf')", "['abdf',undefined,'abd','f']"); 70 71 var regexp16 = /(a|A)(b|B)/; 72 shouldBe("regexp16.exec('abc')", "['ab','a','b']"); 73 74 var regexp17 = /(a|d|q|)x/i; 75 shouldBe("regexp17.exec('bcaDxqy')", "['Dx','D']"); 76 77 var regexp18 = /^.*?(:|$)/; 78 shouldBe("regexp18.exec('Hello: World')", "['Hello:',':']"); 79 80 var regexp19 = /(ab|^.{0,2})bar/; 81 shouldBe("regexp19.exec('barrel')", "['bar','']"); 82 83 var regexp20 = /(?:(?!foo)...|^.{0,2})bar(.*)/; 84 shouldBe("regexp20.exec('barrel')", "['barrel','rel']"); 85 shouldBe("regexp20.exec('2barrel')", "['2barrel','rel']"); 86 87 var regexp21 = /([a-g](b|B)|xyz)/; 88 shouldBe("regexp21.exec('abc')", "['ab','ab','b']"); 89 90 var regexp22 = /(?:^|;)\s*abc=([^;]*)/; 91 shouldBeNull("regexp22.exec('abcdlskfgjdslkfg')"); 92 93 var regexp23 = /"[^<"]*"|'[^<']*'/; 94 shouldBe("regexp23.exec('<html xmlns=\"http://www.w3.org/1999/xhtml\"')", "['\"http://www.w3.org/1999/xhtml\"']"); 95 96 var regexp24 = /^(?:(?=abc)\w{3}:|\d\d)$/; 97 shouldBeNull("regexp24.exec('123')"); 98 99 var regexp25 = /^\s*(\*|[\w\-]+)(\b|$)?/; 100 shouldBe("regexp25.exec('this is a test')", "['this','this',undefined]"); 101 shouldBeNull("regexp25.exec('!this is a test')"); 102 103 var regexp26 = /a(b)(a*)|aaa/; 104 shouldBe("regexp26.exec('aaa')", "['aaa',undefined,undefined]"); 105 106 var regexp27 = new RegExp( 107 "^" + 108 "(?:" + 109 "([^:/?#]+):" + /* scheme */ 110 ")?" + 111 "(?:" + 112 "(//)" + /* authorityRoot */ 113 "(" + /* authority */ 114 "(?:" + 115 "(" + /* userInfo */ 116 "([^:@]*)" + /* user */ 117 ":?" + 118 "([^:@]*)" + /* password */ 119 ")?" + 120 "@" + 121 ")?" + 122 "([^:/?#]*)" + /* domain */ 123 "(?::(\\d*))?" + /* port */ 124 ")" + 125 ")?" + 126 "([^?#]*)" + /*path*/ 127 "(?:\\?([^#]*))?" + /* queryString */ 128 "(?:#(.*))?" /*fragment */ 129 ); 130 shouldBe("regexp27.exec('file:///Users/Someone/Desktop/HelloWorld/index.html')", "['file:///Users/Someone/Desktop/HelloWorld/index.html','file','//','',undefined,undefined,undefined,'',undefined,'/Users/Someone/Desktop/HelloWorld/index.html',undefined,undefined]"); 131 132 var regexp28 = new RegExp( 133 "^" + 134 "(?:" + 135 "([^:/?#]+):" + /* scheme */ 136 ")?" + 137 "(?:" + 138 "(//)" + /* authorityRoot */ 139 "(" + /* authority */ 140 "(" + /* userInfo */ 141 "([^:@]*)" + /* user */ 142 ":?" + 143 "([^:@]*)" + /* password */ 144 ")?" + 145 "@" + 146 ")" + 147 ")?" 148 ); 149 shouldBe("regexp28.exec('file:///Users/Someone/Desktop/HelloWorld/index.html')", "['file:','file',undefined,undefined,undefined,undefined,undefined]"); 150 151 var regexp29 = /^\s*((\[[^\]]+\])|(u?)("[^"]+"))\s*/; 152 shouldBeNull("regexp29.exec('Committer:')"); 153 154 var regexp30 = /^\s*((\[[^\]]+\])|m(u?)("[^"]+"))\s*/; 155 shouldBeNull("regexp30.exec('Committer:')"); 156 157 var regexp31 = /^\s*(m(\[[^\]]+\])|m(u?)("[^"]+"))\s*/; 158 shouldBeNull("regexp31.exec('Committer:')"); 159 160 var regexp32 = /\s*(m(\[[^\]]+\])|m(u?)("[^"]+"))\s*/; 161 shouldBeNull("regexp32.exec('Committer:')"); 162 163 var regexp33 = RegExp('^(?:(?:(a)(xyz|[^>"\'\s]*)?)|(/?>)|.[^\w\s>]*)'); 164 shouldBe("regexp33.exec('> <head>')","['>',undefined,undefined,'>']"); 165 166 var regexp34 = /(?:^|\b)btn-\S+/; 167 shouldBeNull("regexp34.exec('xyz123')"); 168 shouldBe("regexp34.exec('btn-abc')","['btn-abc']"); 169 shouldBeNull("regexp34.exec('btn- abc')"); 170 shouldBeNull("regexp34.exec('XXbtn-abc')"); 171 shouldBe("regexp34.exec('XX btn-abc')","['btn-abc']"); 172 173 var regexp35 = /^((a|b)(x|xxx)|)$/; 174 shouldBe("regexp35.exec('ax')", "['ax','ax','a','x']"); 175 shouldBeNull("regexp35.exec('axx')"); 176 shouldBe("regexp35.exec('axxx')", "['axxx','axxx','a','xxx']"); 177 shouldBe("regexp35.exec('bx')", "['bx','bx','b','x']"); 178 shouldBeNull("regexp35.exec('bxx')"); 179 shouldBe("regexp35.exec('bxxx')", "['bxxx','bxxx','b','xxx']"); 180 181 var regexp36 = /^((\/|\.|\-)(\d\d|\d\d\d\d)|)$/; 182 shouldBe("regexp36.exec('/2011')", "['/2011','/2011','/','2011']"); 183 shouldBe("regexp36.exec('/11')", "['/11','/11','/','11']"); 184 shouldBeNull("regexp36.exec('/123')"); 185 186 var regexp37 = /^([1][0-2]|[0]\d|\d)(\/|\.|\-)([0-2]\d|[3][0-1]|\d)((\/|\.|\-)(\d\d|\d\d\d\d)|)$/; 187 shouldBe("regexp37.exec('7/4/1776')", "['7/4/1776','7','/','4','/1776','/','1776']"); 188 shouldBe("regexp37.exec('07-04-1776')", "['07-04-1776','07','-','04','-1776','-','1776']"); 189 190 var regexp38 = /^(z|(x|xx)|b|)$/; 191 shouldBe("regexp38.exec('xx')", "['xx','xx','xx']"); 192 shouldBe("regexp38.exec('b')", "['b','b',undefined]"); 193 shouldBe("regexp38.exec('z')", "['z','z',undefined]"); 194 shouldBe("regexp38.exec('')", "['','',undefined]"); 195 196 var regexp39 = /(8|((?=P)))?/; 197 shouldBe("regexp39.exec('')", "['',undefined,undefined]"); 198 shouldBe("regexp39.exec('8')", "['8','8',undefined]"); 199 shouldBe("regexp39.exec('zP')", "['',undefined,undefined]"); 200 201 var regexp40 = /((8)|((?=P){4}))?()/; 202 shouldBe("regexp40.exec('')", "['',undefined,undefined,undefined,'']"); 203 shouldBe("regexp40.exec('8')", "['8','8','8',undefined,'']"); 204 shouldBe("regexp40.exec('zPz')", "['',undefined,undefined,undefined,'']"); 205 shouldBe("regexp40.exec('zPPz')", "['',undefined,undefined,undefined,'']"); 206 shouldBe("regexp40.exec('zPPPz')", "['',undefined,undefined,undefined,'']"); 207 shouldBe("regexp40.exec('zPPPPz')", "['',undefined,undefined,undefined,'']"); 208 209 var regexp41 = /(([\w\-]+:\/\/?|www[.])[^\s()<>]+(?:([\w\d]+)|([^\[:punct:\]\s()<>\W]|\/)))/; 210 shouldBe("regexp41.exec('Here is a link: http://www.acme.com/our_products/index.html. That is all we want!')", "['http://www.acme.com/our_products/index.html','http://www.acme.com/our_products/index.html','http://','l',undefined]"); 211 212 var regexp42 = /((?:(4)?))?/; 213 shouldBe("regexp42.exec('')", "['',undefined,undefined]"); 214 shouldBe("regexp42.exec('4')", "['4','4','4']"); 215 shouldBe("regexp42.exec('4321')", "['4','4','4']"); 216 217 shouldBeTrue("/(?!(?=r{0}){2,})|((z)?)?/gi.test('')"); 218 219 var regexp43 = /(?!(?:\1+s))/; 220 shouldBe("regexp43.exec('SSS')", "['']"); 221 222 var regexp44 = /(?!(?:\3+(s+?)))/g; 223 shouldBe("regexp44.exec('SSS')", "['',undefined]"); 224 225 var regexp45 = /((?!(?:|)v{2,}|))/; 226 shouldBeNull("regexp45.exec('vt')"); 227 228 var regexp46 = /(w)(?:5{3}|())|pk/; 229 shouldBeNull("regexp46.exec('5')"); 230 shouldBe("regexp46.exec('pk')", "['pk',undefined,undefined]"); 231 shouldBe("regexp46.exec('Xw555')", "['w555','w',undefined]"); 232 shouldBe("regexp46.exec('Xw55pk5')", "['w','w','']"); 233 234 var regexp47 = /(.*?)(?:(?:\?(.*?)?)?)(?:(?:#)?)$/; 235 shouldBe("regexp47.exec('/www.acme.com/this/is/a/path/file.txt')", "['/www.acme.com/this/is/a/path/file.txt','/www.acme.com/this/is/a/path/file.txt',undefined]"); 236 237 var regexp48 = /^(?:(\w+):\/*([\w\.\-\d]+)(?::(\d+)|)(?=(?:\/|$))|)(?:$|\/?(.*?)(?:\?(.*?)?|)(?:#(.*)|)$)/; 238 /* The regexp on the prior line confuses Xcode syntax highlighting, this coment fixes it! */ 239 shouldBe("regexp48.exec('http://www.acme.com/this/is/a/path/file.txt')", "['http://www.acme.com/this/is/a/path/file.txt','http','www.acme.com',undefined,'this/is/a/path/file.txt',undefined,undefined]"); 240 241 var regexp49 = /(?:([^:]*?)(?:(?:\?(.*?)?)?)(?:(?:#)?)$)|(?:^(?:(\w+):\/*([\w\.\-\d]+)(?::(\d+)|)(?=(?:\/|$))|)(?:$|\/?(.*?)(?:\?(.*?)?|)(?:#(.*)|)$))/; 242 /* The regexp on the prior line confuses Xcode syntax highlighting, this coment fixes it! */ 243 shouldBe("regexp49.exec('http://www.acme.com/this/is/a/path/file.txt')", "['http://www.acme.com/this/is/a/path/file.txt',undefined,undefined,'http','www.acme.com',undefined,'this/is/a/path/file.txt',undefined,undefined]"); 244 245 var regexp50 = /((a)b{28,}c|d)x/; 246 shouldBeNull("regexp50.exec('((a)b{28,}c|d)x')"); 247 shouldBe("regexp50.exec('abbbbbbbbbbbbbbbbbbbbbbbbbbbbcx')", "['abbbbbbbbbbbbbbbbbbbbbbbbbbbbcx', 'abbbbbbbbbbbbbbbbbbbbbbbbbbbbc', 'a']"); 248 shouldBe("regexp50.exec('dx')", "['dx', 'd', undefined]"); 249 250 var s = "((.\s{-}).{28,}\P{Yi}?{,30}\|.)\x9e{-,}\P{Any}"; 251 var regexp51 = new RegExp(s); 252 shouldBeNull("regexp51.exec('abc')"); 253 shouldBe("regexp51.exec(s)", "[')\x9e{-,}P{Any}',')',undefined]"); 254 255 var regexp52 = /(Rob)|(Bob)|(Robert)|(Bobby)/; 256 shouldBe("'Hi Bob'.match(regexp52)", "['Bob',undefined,'Bob',undefined,undefined]"); 257 258 // Test cases discovered by fuzzing that crashed the compiler. 259 var regexp53 = /(?=(?:(?:(gB)|(?!cs|<))((?=(?!v6){0,})))|(?=#)+?)/m; 260 shouldBe("regexp53.exec('#')", "['',undefined,'']"); 261 var regexp54 = /((?:(?:()|(?!))((?=(?!))))|())/m; 262 shouldBe("regexp54.exec('#')", "['','',undefined,undefined,'']"); 263 var regexp55 = /(?:(?:(?:a?|(?:))((?:)))|a?)/m; 264 shouldBe("regexp55.exec('#')", "['','']"); 265 266 // Test evaluation order of empty subpattern alternatives. 267 var regexp56 = /(|a)/; 268 shouldBe("regexp56.exec('a')", "['','']"); 269 var regexp57 = /(a|)/; 270 shouldBe("regexp57.exec('a')", "['a','a']"); 271 272 // Tests that non-greedy repeat quantified parentheses will backtrack through multiple frames of subpattern matches. 273 var regexp58 = /a|b(?:[^b])*?c/; 274 shouldBe("regexp58.exec('badbc')", "['a']"); 275 var regexp59 = /(X(?:.(?!X))*?Y)|(Y(?:.(?!Y))*?Z)/g; 276 shouldBe("'Y aaa X Match1 Y aaa Y Match2 Z'.match(regexp59)", "['X Match1 Y','Y Match2 Z']"); 277