| 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: > |
| 6 |
Test the groups object with matched and unmatched named captures. |
| 7 |
esid: sec-regexpbuiltinexec |
| 8 |
features: [regexp-named-groups] |
| 9 |
info: | |
| 10 |
Runtime Semantics: RegExpBuiltinExec ( R, S ) |
| 11 |
24. If _R_ contains any |GroupName|, then |
| 12 |
a. Let _groups_ be ObjectCreate(*null*). |
| 13 |
25. Else, |
| 14 |
a. Let _groups_ be *undefined*. |
| 15 |
26. Perform ! CreateDataProperty(_A_, `"groups"`, _groups_). |
| 16 |
---*/ |
| 17 |
|
| 18 |
const re = /(?<a>a).|(?<x>x)/; |
| 19 |
const result = re.exec("ab"); |
| 20 |
assert.sameValue(Object.getPrototypeOf(result), Array.prototype); |
| 21 |
assert(result.hasOwnProperty("groups")); |
| 22 |
assert.sameValue("ab", result[0]); |
| 23 |
assert.sameValue("a", result[1]); |
| 24 |
assert.sameValue(undefined, result[2]); |
| 25 |
assert.sameValue(0, result.index); |
| 26 |
assert.sameValue("a", result.groups.a); |
| 27 |
assert.sameValue(undefined, result.groups.x); |
| 28 |
|
| 29 |
// `a` is a matched named capture, `b` is an unmatched named capture, and `z` |
| 30 |
// is not a named capture. |
| 31 |
Array.prototype.groups = { a: "b", x: "y", z: "z" }; |
| 32 |
assert.sameValue("a", "ab".replace(re, "$<a>")); |
| 33 |
assert.sameValue("", "ab".replace(re, "$<x>")); |
| 34 |
assert.sameValue("", "ab".replace(re, "$<z>")); |
| 35 |
Array.prototype.groups = undefined; |