A character class escape is an escape sequence that represents a set of characters.
A character class escape is an escape sequence that represents a set of characters.
\d, \D \s, \S \w, \W
Note: ,
is not part of the syntax.
Unlike character escapes, character class escapes represent a predefined set of characters, much like a character class. The following character classes are supported:
\d
Matches any digit character. Equivalent to [0-9]
.
\w
Matches any word character, where a word character includes letters (A–Z, a–z), numbers (0–9), and underscore (_). If the regex is Unicode-aware and the i
flag is set, it also matches other Unicode characters that get canonicalized to one of the characters above through case folding.
\s
Matches any whitespace or line terminator character.
The uppercase forms \D
, \W
, and \S
create complement character classes for \d
, \w
, and \s
, respectively. They match any character that is not in the set of characters matched by the lowercase form.
Unicode character class escapes start with \p
and \P
, but they are only supported in Unicode-aware mode. In Unicode-unaware mode, they are identity escapes for the p
or P
character.
Character class escapes can be used in character classes. However, they cannot be used as boundaries of character ranges, which is only allowed as a deprecated syntax for web compatibility, and you should not rely on it.
The following example splits a string into an array of words, supporting all kinds of whitespace separators:
function splitWords(str) { return str.split(/\s+/); } splitWords(`Look at the stars Look how they\tshine for you`); // ['Look', 'at', 'the', 'stars', 'Look', 'how', 'they', 'shine', 'for', 'you']
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
Character_class_escape |
1 | 12 | 1 | 5 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1.0 | 0.10.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape