-
Notifications
You must be signed in to change notification settings - Fork 22.5k
/
index.md
76 lines (55 loc) · 2.08 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
title: "SyntaxError: invalid capture group name in regular expression"
slug: Web/JavaScript/Reference/Errors/Regex_invalid_capture_group_name
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript exception "invalid capture group name in regular expression" occurs when a [named capturing group](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_capturing_group) or [named backreference](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_backreference) contains an invalid [identifier](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers).
## Message
```plain
SyntaxError: Invalid regular expression: /(?<1>)/: Invalid capture group name (V8-based)
SyntaxError: invalid capture group name in regular expression (Firefox)
SyntaxError: Invalid regular expression: invalid group specifier name (Safari)
```
## Error type
{{jsxref("SyntaxError")}}
## What went wrong?
Each named capturing group must have a name that is a valid identifier. You cannot use arbitrary strings as the group identifier.
## Examples
### Invalid cases
```js example-bad
/(?<1>\d+) (?<2>\d+)/;
```
Or you might be building the regex dynamically:
```js example-bad
const tokenTypes = {
"number literal": /\d+/,
"string literal": /".+?"/,
identifier: /[a-zA-Z_]\w*/,
};
const tokenPattern = new RegExp(
Object.entries(tokenTypes)
.map(([name, pattern]) => `(?<${name}>${pattern.source})`)
.join("|"),
);
```
### Valid cases
```js example-good
/(?<group1>\d+) (?<group2>\d+)/;
```
If the regex is built dynamically, make sure the names are all valid identifiers. For example:
```js example-good
const tokenTypes = {
numberLiteral: /\d+/,
stringLiteral: /".+?"/,
identifier: /[a-zA-Z_]\w*/,
};
const tokenPattern = new RegExp(
Object.entries(tokenTypes)
.map(([name, pattern]) => `(?<${name}>${pattern.source})`)
.join("|"),
);
```
## See also
- [Regular expressions](/en-US/docs/Web/JavaScript/Reference/Regular_expressions)
- [Named capturing group: `(?<name>...)`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_capturing_group)