Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
/\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

JS Shorthand classes and sets

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Unicode property escapes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
\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

  • \w and \d are ASCII only. \w does not match é, ü or 東, even with the u flag. For names and words in other languages use \p{L} (letters) and \p{N} (numbers) with the u flag.
  • 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 because a comes after Z. [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 the s flag. Prefer . with the s flag today.
  • Property escapes need u or v. Without either flag, \p{L} is just the text p{L}. The newer v flag 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 i makes [a-z] match uppercase letters too, and a negated set is evaluated after case folding.
  • [\b] is backspace. Inside a class \b means 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.