| 1 |
// Copyright 2017 the V8 project authors. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
description: Named backreferences in Unicode RegExps |
| 6 |
esid: sec-atomescape |
| 7 |
info: | |
| 8 |
The production AtomEscape :: [+N] k GroupName evaluates as follows: |
| 9 |
|
| 10 |
1. Search the enclosing RegExp for an instance of a GroupSpecifier for an |
| 11 |
RegExpIdentifierName which has a StringValue equal to the StringValue |
| 12 |
of the RegExpIdentifierName contained in GroupName. |
| 13 |
2. Assert: A unique such GroupSpecifier is found. |
| 14 |
3. Let parenIndex be the number of left capturing parentheses in the entire |
| 15 |
regular expression that occur to the left of the located GroupSpecifier. |
| 16 |
This is the total number of times the Atom::(GroupSpecifierDisjunction) |
| 17 |
production is expanded prior to that production's Term plus the total |
| 18 |
number of Atom :: (GroupSpecifierDisjunction) productions enclosing this Term. |
| 19 |
4. Call BackreferenceMatcher(parenIndex) and return its Matcher result. |
| 20 |
features: [regexp-named-groups] |
| 21 |
includes: [compareArray.js] |
| 22 |
---*/ |
| 23 |
|
| 24 |
// Named references. |
| 25 |
assert(compareArray(["bab", "b"], "bab".match(/(?<b>.).\k<b>/u))); |
| 26 |
assert.sameValue(null, "baa".match(/(?<b>.).\k<b>/u)); |
| 27 |
|
| 28 |
// Reference inside group. |
| 29 |
assert(compareArray(["bab", "b"], "bab".match(/(?<a>\k<a>\w)../u))); |
| 30 |
assert.sameValue("b", "bab".match(/(?<a>\k<a>\w)../u).groups.a); |
| 31 |
|
| 32 |
// Reference before group. |
| 33 |
assert(compareArray(["bab", "b"], "bab".match(/\k<a>(?<a>b)\w\k<a>/u))); |
| 34 |
assert.sameValue("b", "bab".match(/\k<a>(?<a>b)\w\k<a>/u).groups.a); |
| 35 |
assert(compareArray(["bab", "b", "a"], "bab".match(/(?<b>b)\k<a>(?<a>a)\k<b>/u))); |
| 36 |
let {a, b} = "bab".match(/(?<b>b)\k<a>(?<a>a)\k<b>/u).groups; |
| 37 |
assert.sameValue(a, "a"); |
| 38 |
assert.sameValue(b, "b"); |
| 39 |
|
| 40 |
assert(compareArray(["bab", "b"], "bab".match(/\k<a>(?<a>b)\w\k<a>/))); |
| 41 |
assert(compareArray(["bab", "b", "a"], "bab".match(/(?<b>b)\k<a>(?<a>a)\k<b>/))); |
| 42 |
|
| 43 |
// Reference properties. |
| 44 |
assert.sameValue("a", /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.a); |
| 45 |
assert.sameValue("b", /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.b); |
| 46 |
assert.sameValue(undefined, /(?<a>a)(?<b>b)\k<a>/u.exec("aba").groups.c); |
| 47 |
assert.sameValue(undefined, /(?<a>a)(?<b>b)\k<a>|(?<c>c)/u.exec("aba").groups.c); |