SyntaxError: invalid character in class in regular expression
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
The JavaScript exception "invalid character in class in regular expression" occurs when a character appears in a [v-mode character class](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class#v-mode_character_class) but it's not allowed to appear literally.
Message
SyntaxError: Invalid regular expression: /[|]/v: Invalid character in character class (V8-based)
SyntaxError: invalid character in class in regular expression (Firefox)
SyntaxError: Invalid regular expression: invalid class set character (Safari)Error type
{{jsxref("SyntaxError")}}
What went wrong?
Normally, character classes can contain almost all characters literally. However, the v mode made the character class syntax more sophisticated, and in order to leave room for future syntax extensions, some syntax characters are forbidden from appearing literally in a character class. They include: (, ), [, ], {, }, /, -, |. If you want to match these literal characters, escape them; for example: /[\|]/v.
Examples
Invalid cases
/[(){}]/v;Valid cases
<!-- Note: the {} need to be double-escaped, once for Yari -->
/[\(\)\\{\\}]/v;See also
- Regular expressions
- [Character class:
[...],[^...]](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class)