Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
50 lines (34 loc) · 2.36 KB
/
Copy pathindex.md
File metadata and controls
50 lines (34 loc) · 2.36 KB
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
---
title: "SyntaxError: invalid identity escape in regular expression"
slug: Web/JavaScript/Reference/Errors/Regex_invalid_identity_escape
page-type: javascript-error
sidebar: jssidebar
---
The JavaScript exception "invalid identity escape in regular expression" occurs when a [Unicode-aware](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode#unicode-aware_mode) regular expression pattern contains an [escape sequence](/en-US/docs/Web/JavaScript/Reference/Regular_expressions#escape_sequences) that does not represent a recognized escape sequence.
## Message
```plain
SyntaxError: Invalid regular expression: /\q/u: Invalid escape (V8-based)
SyntaxError: invalid identity escape in regular expression (Firefox)
SyntaxError: Invalid regular expression: invalid escaped character for Unicode pattern (Safari)
```
## Error type
{{jsxref("SyntaxError")}}
## What went wrong?
In Unicode-unaware mode, `\` could be used to escape any character, including those without a defined meaning. In these cases, the escaped character represents itself. For example, `\q` would match the character `q`. This severely limits the ability to add new escape sequences in the future, so in Unicode-aware mode, only recognized escape sequences are allowed. Do not add `\` redundantly.
This error is also thrown when the `\x` [character escape](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape) is not followed by two hexadecimal digits.
For an exhaustive list of available escape sequences, see the [regular expressions reference](/en-US/docs/Web/JavaScript/Reference/Regular_expressions#escape_sequences). For a table of which characters can appear literally escaped or unescaped in each context, see [literal characters](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Literal_character).
## Examples
### Invalid cases
```js example-bad
/[\f\v\n\t\ ]/u;
```
### Valid cases
```js example-good
// There's no need to escape the space
/[\f\v\n\t ]/u;
```
## See also
- [Regular expressions](/en-US/docs/Web/JavaScript/Reference/Regular_expressions)
- [Escape sequences](/en-US/docs/Web/JavaScript/Reference/Regular_expressions#escape_sequences)
- [Character escape: `\n`, `\u{...}`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape)
- [Literal character: `a`, `b`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Literal_character)