JavaScript Reference · RegExp
Regular expression character classes
Reference for JavaScript regular expression character classes: \d, \w, \s, the dot, [abc] sets, negated sets, ranges and Unicode property escapes.
A character class matches one character out of a set of possibilities. JavaScript offers three kinds: shorthand classes such as \d and \w, bracketed sets such as [aeiou] or [^0-9], and Unicode property escapes such as \p{L}, which describe characters by what they are rather than by listing them.
A class always consumes exactly one character (one code point when the u or v flag is on). To match several characters in a row, follow the class with a quantifier.
/\d{4}/ // four digits
/[aeiou]/i // any vowel, either case
/[^\s]+/ // one or more non-whitespace characters
/\p{L}+/u // one or more letters in any script
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
\d \D // digit / non-digit \w \W // word character / non-word character \s \S // whitespace / non-whitespace . // any character except line terminators [abc] // any one of a, b, c [a-z0-9] // ranges [^abc] // any character except a, b, c \p{Property} // Unicode property (needs u or v flag) \P{Property} // the complement
| Class | Matches |
|---|---|
\d |
An ASCII digit, the same as [0-9]. |
\w |
An ASCII letter, digit or underscore, the same as [A-Za-z0-9_]. |
\s |
Any whitespace: space, tab, line breaks, no-break space and other Unicode spaces. |
. |
Any character except \n, \r,
and
; with the s flag it matches those too. |
\D, \W, \S |
The complement of the lowercase class. They do match line breaks. |
[...] |
One character listed or covered by a range inside the brackets. |
[^...] |
One character not listed in the brackets. A negated set matches line breaks too. |
\p{...} |
A character with a Unicode property, such as \p{L} (letter), \p{Lu} (uppercase letter), \p{Nd} (decimal digit), \p{Script=Greek} or \p{Emoji}. |
Usage notes
\wand\dare ASCII only.\wdoes not matché,üor東, even with theuflag. For names and words in other languages use\p{L}(letters) and\p{N}(numbers) with theuflag.- Most characters are literal inside brackets. In
[.*+?]the dot and the quantifier characters have no special meaning. Only\,], a leading^and a-between two characters are special. Put-first or last ([-_],[a-z-]) or escape it ([a\-z]) to match a hyphen. - Shorthands work inside sets.
[\w.-]matches a word character, a dot or a hyphen, and[\d\s]matches digits and whitespace. - A range goes by code point.
[a-Z]is a syntax error becauseacomes afterZ.[A-z]is legal but also includes[,\,],^,_and the backtick, so avoid it. [^...]is not "optional absence". It still needs a character to match, so/a[^b]/does not match the string"a"alone.[\s\S]and[^]match literally anything, a trick from before thesflag. Prefer.with thesflag today.- Property escapes need
uorv. Without either flag,\p{L}is just the textp{L}. The newervflag adds set operations inside classes, such as[\p{L}--[a-z]](subtraction) and[\p{L}&&\p{ASCII}](intersection), and can match properties of strings such as\p{RGI_Emoji}. - Case-insensitive matching with
imakes[a-z]match uppercase letters too, and a negated set is evaluated after case folding. [\b]is backspace. Inside a class\bmeans the backspace character, not a word boundary (see anchors).
Browser support
Shorthand classes and bracketed sets work everywhere. Unicode property escapes (with the u flag) are supported by all current browsers; the v flag and its set notation are newer, so check a compatibility table before relying on them.
Related
- Regular expression flags –
u,v,iands - Quantifiers – repeat a class
- Anchors and boundaries
- Capture groups
- Lesson: Regular Expressions
- Lesson: Regex in Practice