1// Copyright 2015 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Flags: --harmony-regexp-lookbehind 6 7// Simple fixed-length matches. 8assertEquals(["a"], "a".match(/^.(?<=a)/)); 9assertNull("b".match(/^.(?<=a)/)); 10assertEquals(["foo"], "foo1".match(/^f..(?<=.oo)/)); 11assertEquals(["foo"], "foo2".match(/^f\w\w(?<=\woo)/)); 12assertNull("boo".match(/^f\w\w(?<=\woo)/)); 13assertNull("fao".match(/^f\w\w(?<=\woo)/)); 14assertNull("foa".match(/^f\w\w(?<=\woo)/)); 15assertEquals(["def"], "abcdef".match(/(?<=abc)\w\w\w/)); 16assertEquals(["def"], "abcdef".match(/(?<=a.c)\w\w\w/)); 17assertEquals(["def"], "abcdef".match(/(?<=a\wc)\w\w\w/)); 18assertEquals(["cde"], "abcdef".match(/(?<=a[a-z])\w\w\w/)); 19assertEquals(["def"], "abcdef".match(/(?<=a[a-z][a-z])\w\w\w/)); 20assertEquals(["def"], "abcdef".match(/(?<=a[a-z]{2})\w\w\w/)); 21assertEquals(["bcd"], "abcdef".match(/(?<=a{1})\w\w\w/)); 22assertEquals(["cde"], "abcdef".match(/(?<=a{1}b{1})\w\w\w/)); 23assertEquals(["def"], "abcdef".match(/(?<=a{1}[a-z]{2})\w\w\w/)); 24 25// Variable-length matches. 26assertEquals(["def"], "abcdef".match(/(?<=[a|b|c]*)[^a|b|c]{3}/)); 27assertEquals(["def"], "abcdef".match(/(?<=\w*)[^a|b|c]{3}/)); 28 29// Start of line matches. 30assertEquals(["def"], "abcdef".match(/(?<=^abc)def/)); 31assertEquals(["def"], "abcdef".match(/(?<=^[a-c]{3})def/)); 32assertEquals(["def"], "xyz\nabcdef".match(/(?<=^[a-c]{3})def/m)); 33assertEquals(["ab", "cd", "efg"], "ab\ncd\nefg".match(/(?<=^)\w+/gm)); 34assertEquals(["ab", "cd", "efg"], "ab\ncd\nefg".match(/\w+(?<=$)/gm)); 35assertEquals(["ab", "cd", "efg"], "ab\ncd\nefg".match(/(?<=^)\w+(?<=$)/gm)); 36assertNull("abcdef".match(/(?<=^[^a-c]{3})def/)); 37assertNull("foooo".match(/"^foooo(?<=^o+)$/)); 38assertNull("foooo".match(/"^foooo(?<=^o*)$/)); 39assertEquals(["foo"], "foo".match(/^foo(?<=^fo+)$/)); 40assertEquals(["foooo"], "foooo".match(/^foooo(?<=^fo*)/)); 41assertEquals(["foo", "f"], "foo".match(/^(f)oo(?<=^\1o+)$/)); 42assertEquals(["foo", "f"], "foo".match(/^(f)oo(?<=^\1o+)$/i)); 43assertEquals(["foo\u1234", "f"], "foo\u1234".match(/^(f)oo(?<=^\1o+).$/i)); 44assertEquals(["def"], "abcdefdef".match(/(?<=^\w+)def/)); 45assertEquals(["def", "def"], "abcdefdef".match(/(?<=^\w+)def/g)); 46 47// Word boundary matches. 48assertEquals(["def"], "abc def".match(/(?<=\b)[d-f]{3}/)); 49assertEquals(["def"], "ab cdef".match(/(?<=\B)\w{3}/)); 50assertEquals(["def"], "ab cdef".match(/(?<=\B)(?<=c(?<=\w))\w{3}/)); 51assertNull("abcdef".match(/(?<=\b)[d-f]{3}/)); 52 53// Negative lookbehind. 54assertEquals(["abc"], "abcdef".match(/(?<!abc)\w\w\w/)); 55assertEquals(["abc"], "abcdef".match(/(?<!a.c)\w\w\w/)); 56assertEquals(["abc"], "abcdef".match(/(?<!a\wc)\w\w\w/)); 57assertEquals(["abc"], "abcdef".match(/(?<!a[a-z])\w\w\w/)); 58assertEquals(["abc"], "abcdef".match(/(?<!a[a-z]{2})\w\w\w/)); 59assertNull("abcdef".match(/(?<!abc)def/)); 60assertNull("abcdef".match(/(?<!a.c)def/)); 61assertNull("abcdef".match(/(?<!a\wc)def/)); 62assertNull("abcdef".match(/(?<!a[a-z][a-z])def/)); 63assertNull("abcdef".match(/(?<!a[a-z]{2})def/)); 64assertNull("abcdef".match(/(?<!a{1}b{1})cde/)); 65assertNull("abcdef".match(/(?<!a{1}[a-z]{2})def/)); 66 67// Capturing matches. 68assertEquals(["def", "c"], "abcdef".match(/(?<=(c))def/)); 69assertEquals(["def", "bc"], "abcdef".match(/(?<=(\w{2}))def/)); 70assertEquals(["def", "bc", "c"], "abcdef".match(/(?<=(\w(\w)))def/)); 71assertEquals(["def", "a"], "abcdef".match(/(?<=(\w){3})def/)); 72assertEquals(["d", "bc", undefined], "abcdef".match(/(?<=(bc)|(cd))./)); 73assertEquals(["c", "a", undefined], 74 "abcdef".match(/(?<=([ab]{1,2})\D|(abc))\w/)); 75assertEquals(["ab", "a", "b"], "abcdef".match(/\D(?<=([ab]+))(\w)/)); 76assertEquals(["c", "d"], "abcdef".match(/(?<=b|c)\w/g)); 77assertEquals(["cd", "ef"], "abcdef".match(/(?<=[b-e])\w{2}/g)); 78 79// Captures inside negative lookbehind. (They never capture.) 80assertEquals(["de", undefined], "abcdef".match(/(?<!(^|[ab]))\w{2}/)); 81 82// Nested lookaround. 83assertEquals(["ef"], "abcdef".match(/(?<=ab(?=c)\wd)\w\w/)); 84assertEquals(["ef", "bc"], "abcdef".match(/(?<=a(?=([^a]{2})d)\w{3})\w\w/)); 85assertEquals(["ef", "bc"], 86 "abcdef".match(/(?<=a(?=([bc]{2}(?<!a{2}))d)\w{3})\w\w/)); 87assertNull("abcdef".match(/(?<=a(?=([bc]{2}(?<!a*))d)\w{3})\w\w/)); 88assertEquals(["faaa"], "faaao".match(/^faaao?(?<=^f[oa]+(?=o))/)); 89 90// Back references. 91assertEquals(["b", "b", "bb"], "abb".match(/(.)(?<=(\1\1))/)); 92assertEquals(["B", "B", "bB"], "abB".match(/(.)(?<=(\1\1))/i)); 93assertEquals(["aB", "aB", "a"], "aabAaBa".match(/((\w)\w)(?<=\1\2\1)/i)); 94assertEquals(["Ba", "Ba", "a"], "aabAaBa".match(/(\w(\w))(?<=\1\2\1)/i)); 95assertEquals(["b", "b", "B"], "abaBbAa".match(/(?=(\w))(?<=(\1))./i)); 96assertEquals(["foo", "'", "foo"], " 'foo' ".match(/(?<=(.))(\w+)(?=\1)/)); 97assertEquals(["foo", "\"", "foo"], " \"foo\" ".match(/(?<=(.))(\w+)(?=\1)/)); 98assertNull(" .foo\" ".match(/(?<=(.))(\w+)(?=\1)/)); 99assertNull("ab".match(/(.)(?<=\1\1\1)/)); 100assertNull("abb".match(/(.)(?<=\1\1\1)/)); 101assertEquals(["b", "b"], "abbb".match(/(.)(?<=\1\1\1)/)); 102assertNull("ab".match(/(..)(?<=\1\1\1)/)); 103assertNull("abb".match(/(..)(?<=\1\1\1)/)); 104assertNull("aabb".match(/(..)(?<=\1\1\1)/)); 105assertNull("abab".match(/(..)(?<=\1\1\1)/)); 106assertNull("fabxbab".match(/(..)(?<=\1\1\1)/)); 107assertNull("faxabab".match(/(..)(?<=\1\1\1)/)); 108assertEquals(["ab", "ab"], "fababab".match(/(..)(?<=\1\1\1)/)); 109 110// Back references to captures inside the lookbehind. 111assertEquals(["d", "C"], "abcCd".match(/(?<=\1(\w))d/i)); 112assertEquals(["d", "x"], "abxxd".match(/(?<=\1([abx]))d/)); 113assertEquals(["c", "ab"], "ababc".match(/(?<=\1(\w+))c/)); 114assertEquals(["c", "b"], "ababbc".match(/(?<=\1(\w+))c/)); 115assertNull("ababdc".match(/(?<=\1(\w+))c/)); 116assertEquals(["c", "abab"], "ababc".match(/(?<=(\w+)\1)c/)); 117 118// Alternations are tried left to right, 119// and we do not backtrack into a lookbehind. 120assertEquals(["xabcd", "cd", ""], "xabcd".match(/.*(?<=(..|...|....))(.*)/)); 121assertEquals(["xabcd", "bcd", ""], "xabcd".match(/.*(?<=(xx|...|....))(.*)/)); 122assertEquals(["xxabcd", "bcd", ""], "xxabcd".match(/.*(?<=(xx|...))(.*)/)); 123assertEquals(["xxabcd", "xx", "abcd"], "xxabcd".match(/.*(?<=(xx|xxx))(.*)/)); 124 125// We do not backtrack into a lookbehind. 126// The lookbehind captures "abc" so that \1 does not match. We do not backtrack 127// to capture only "bc" in the lookbehind. 128assertNull("abcdbc".match(/(?<=([abc]+)).\1/)); 129 130// Greedy loop. 131assertEquals(["c", "bbbbbb"], "abbbbbbc".match(/(?<=(b+))c/)); 132assertEquals(["c", "b1234"], "ab1234c".match(/(?<=(b\d+))c/)); 133assertEquals(["c", "b12b23b34"], "ab12b23b34c".match(/(?<=((?:b\d{2})+))c/)); 134 135// Sticky 136var re1 = /(?<=^(\w+))def/g; 137assertEquals(["def", "abc"], re1.exec("abcdefdef")); 138assertEquals(["def", "abcdef"], re1.exec("abcdefdef")); 139var re2 = /\Bdef/g; 140assertEquals(["def"], re2.exec("abcdefdef")); 141assertEquals(["def"], re2.exec("abcdefdef")); 142 143// Misc 144assertNull("abcdef".match(/(?<=$abc)def/)); 145assertEquals(["foo"], "foo".match(/^foo(?<=foo)$/)); 146assertEquals(["foo"], "foo".match(/^f.o(?<=foo)$/)); 147assertNull("fno".match(/^f.o(?<=foo)$/)); 148assertNull("foo".match(/^foo(?<!foo)$/)); 149assertNull("foo".match(/^f.o(?<!foo)$/)); 150assertEquals(["fno"], "fno".match(/^f.o(?<!foo)$/)); 151assertEquals(["foooo"], "foooo".match(/^foooo(?<=fo+)$/)); 152assertEquals(["foooo"], "foooo".match(/^foooo(?<=fo*)$/)); 153assertEquals(["abc", "abc"], /(abc\1)/.exec("abc")); 154assertEquals(["abc", "abc"], /(abc\1)/.exec("abc\u1234")); 155assertEquals(["abc", "abc"], /(abc\1)/i.exec("abc")); 156assertEquals(["abc", "abc"], /(abc\1)/i.exec("abc\u1234")); 157var oob_subject = "abcdefghijklmnabcdefghijklmn".substr(14); 158assertNull(oob_subject.match(/(?=(abcdefghijklmn))(?<=\1)a/i)); 159assertNull(oob_subject.match(/(?=(abcdefghijklmn))(?<=\1)a/)); 160 161// Mutual recursive capture/back references 162assertEquals(["cacb", "a", ""], /(?<=a(.\2)b(\1)).{4}/.exec("aabcacbc")); 163assertEquals(["b", "ac", "ac"], /(?<=a(\2)b(..\1))b/.exec("aacbacb")); 164assertEquals(["x", "aa"], /(?<=(?:\1b)(aa))./.exec("aabaax")); 165assertEquals(["x", "aa"], /(?<=(?:\1|b)(aa))./.exec("aaaax")); 166