// Copyright 2015 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Flags: --harmony-regexp-named-captures // Malformed named captures. assertThrows("/(?<>a)/u"); // Empty name. assertThrows("/(?a)/u"); // Name starting with digits. assertThrows("/(?<:a>a)/u"); // Name starting with invalid char. assertThrows("/(?a)/u"); // Name containing with invalid char. assertThrows("/(?a)(?a)/u"); // Duplicate name. assertThrows("/(?a)(?b)(?a)/u"); // Duplicate name. assertThrows("/\\k/u"); // Invalid reference. assertThrows("/(?a)\\k/u"); // Invalid reference. assertThrows("/(?a)\\k/u"); // Invalid reference. assertThrows("/\\k(?a)/u"); // Invalid reference. // Fallback behavior in non-unicode mode. assertThrows("/(?<>a)/", SyntaxError); assertThrows("/(?a)/", SyntaxError); assertThrows("/(?<:a>a)/", SyntaxError); assertThrows("/(?a)/", SyntaxError); assertThrows("/(?a)(?a)/", SyntaxError); assertThrows("/(?a)(?b)(?a)/", SyntaxError); assertThrows("/(?a)\\k/", SyntaxError); assertThrows("/(?a)\\k/", SyntaxError); assertEquals(["k"], "xxxkxxx".match(/\k/)); assertEquals(["kxxx".match(/\ka)/u)); assertEquals(["a", "a"], "bab".match(/(?a)/u)); assertEquals(["a", "a"], "bab".match(/(?<_>a)/u)); assertEquals(["a", "a"], "bab".match(/(?<$>a)/u)); assertEquals(["bab", "a"], "bab".match(/.(?<$>a)./u)); assertEquals(["bab", "a", "b"], "bab".match(/.(?a)(.)/u)); assertEquals(["bab", "a", "b"], "bab".match(/.(?a)(?.)/u)); assertEquals(["bab", "ab"], "bab".match(/.(?\w\w)/u)); assertEquals(["bab", "bab"], "bab".match(/(?\w\w\w)/u)); assertEquals(["bab", "ba", "b"], "bab".match(/(?\w\w)(?\w)/u)); assertEquals("bab".match(/(a)/u), "bab".match(/(?a)/u)); assertEquals("bab".match(/(a)/u), "bab".match(/(?a)/u)); assertEquals("bab".match(/(a)/u), "bab".match(/(?<_>a)/u)); assertEquals("bab".match(/(a)/u), "bab".match(/(?<$>a)/u)); assertEquals("bab".match(/.(a)./u), "bab".match(/.(?<$>a)./u)); assertEquals("bab".match(/.(a)(.)/u), "bab".match(/.(?a)(.)/u)); assertEquals("bab".match(/.(a)(.)/u), "bab".match(/.(?a)(?.)/u)); assertEquals("bab".match(/.(\w\w)/u), "bab".match(/.(?\w\w)/u)); assertEquals("bab".match(/(\w\w\w)/u), "bab".match(/(?\w\w\w)/u)); assertEquals("bab".match(/(\w\w)(\w)/u), "bab".match(/(?\w\w)(?\w)/u)); assertEquals(["bab", "b"], "bab".match(/(?b).\1/u)); assertEquals(["baba", "b", "a"], "baba".match(/(.)(?a)\1\2/u)); assertEquals(["baba", "b", "a", "b", "a"], "baba".match(/(.)(?a)(?\1)(\2)/u)); assertEquals(["<)a/u)); assertEquals([">a", ">"], ">a".match(/(?>)a/u)); // Named references. assertEquals(["bab", "b"], "bab".match(/(?.).\k/u)); assertNull("baa".match(/(?.).\k/u)); // Nested groups. assertEquals(["bab", "bab", "ab", "b"], "bab".match(/(?.(?.(?.)))/u)); // Reference inside group. assertEquals(["bab", "b"], "bab".match(/(?\k\w)../u)); // Reference before group. assertEquals(["bab", "b"], "bab".match(/\k(?b)\w\k/u)); assertEquals(["bab", "b", "a"], "bab".match(/(?b)\k(?a)\k/u)); // Reference properties. assertEquals("a", /(?a)(?b)\k/u.exec("aba").group.a); assertEquals("b", /(?a)(?b)\k/u.exec("aba").group.b); assertEquals(undefined, /(?a)(?b)\k/u.exec("aba").group.c); assertFalse(/(?a)(?b)\k/u.exec("aba").group.hasOwnProperty("c")); assertEquals(undefined, /(?a)(?b)\k|(?c)/u.exec("aba").group.c); assertTrue(/(?a)(?b)\k|(?c)/u .exec("aba").group.hasOwnProperty("c")); // Unicode names. assertEquals("a", /(?<π>a)/u.exec("bab").group.π); assertEquals("a", /(?<\u{03C0}>a)/u.exec("bab").group.\u03C0); assertEquals("a", /(?<$>a)/u.exec("bab").group.$); assertEquals("a", /(?<_>a)/u.exec("bab").group._); assertEquals("a", /(?<$𐒤>a)/u.exec("bab").group.$𐒤); assertEquals("a", /(?<_\u200C>a)/u.exec("bab").group._\u200C); assertEquals("a", /(?<_\u200D>a)/u.exec("bab").group._\u200D); assertEquals("a", /(?<ಠ_ಠ>a)/u.exec("bab").group.ಠ_ಠ); assertThrows('/(?<❤>a)/u', SyntaxError); assertThrows('/(?<𐒤>a)/u', SyntaxError); // ID_Continue but not ID_Start. // Capture name conflicts. assertThrows(() => /(?<__proto__>a)/u, SyntaxError); assertEquals("a", /(?<__proto_>a)/u.exec("a").group.__proto_); assertEquals("a", /(?<__proto___>a)/u.exec("a").group.__proto___);