Reference
Token Reference

Token Reference

Quick reference for all regex tokens and syntax supported in JavaScript.

Character Classes

TokenMeaningExample
.Any character (except newline)a.c matches abc, a1c
\dAny digit (0-9)\d+ matches 123
\DAny non-digit\D+ matches abc
\wWord character (a-z, A-Z, 0-9, _)\w+ matches hello_123
\WNon-word character\W+ matches !@#
\sWhitespace (space, tab, newline)\s+ matches
\SNon-whitespace\S+ matches hello
[abc]Any character in set[aeiou] matches vowels
[^abc]Any character not in set[^0-9] matches non-digits
[a-z]Character range[a-zA-Z] matches letters

Anchors

TokenMeaningExample
^Start of string (or line with m)^hello matches at start
$End of string (or line with m)world$ matches at end
\bWord boundary\bword\b matches whole word
\BNon-word boundary\Bword matches within word

Quantifiers

TokenMeaningExample
*Zero or morea* matches ``, a, aaa
+One or morea+ matches a, aaa
?Zero or onecolou?r matches color, colour
{n}Exactly na{3} matches aaa
{n,}n or morea{2,} matches aa, aaa, ...
{n,m}Between n and ma{2,4} matches aa, aaa, aaaa

Lazy Quantifiers

Add ? after any quantifier to make it lazy (match minimum):

TokenMeaning
*?Zero or more (lazy)
+?One or more (lazy)
??Zero or one (lazy)
{n,m}?Between n and m (lazy)

Example:

Greedy: <.*>  on "<a><b>" matches "<a><b>"
Lazy:   <.*?> on "<a><b>" matches "<a>"

Groups

TokenMeaningExample
(...)Capture group(ab)+ captures ab
(?:...)Non-capturing group(?:ab)+ groups without capture
(?<name>...)Named capture group(?<year>\d{4})
\1, \2Backreference(.)\1 matches aa, bb
\k<name>Named backreference\k<year>

Lookarounds

TokenMeaningExample
(?=...)Positive lookaheada(?=b) matches a in ab
(?!...)Negative lookaheada(?!b) matches a not before b
(?<=...)Positive lookbehind(?<=a)b matches b in ab
(?<!...)Negative lookbehind(?<!a)b matches b not after a

Alternation

TokenMeaningExample
|ORcat|dog matches cat or dog

Escape Sequences

TokenMeaning
\\Literal backslash
\.Literal dot
\*Literal asterisk
\+Literal plus
\?Literal question mark
\^Literal caret
\$Literal dollar
\[Literal bracket
\]Literal bracket
\{Literal brace
\}Literal brace
\(Literal parenthesis
\)Literal parenthesis
|Literal pipe
\/Literal slash

Special Characters

TokenMeaning
\nNewline
\rCarriage return
\tTab
\vVertical tab
\fForm feed
\0Null character
\xNNHex character (e.g., \x41 = A)
\uNNNNUnicode character (e.g., \u0041 = A)
\u{NNNN}Unicode code point (requires u flag)

Unicode Properties (with u flag)

TokenMeaning
\p{L}Any letter
\p{Ll}Lowercase letter
\p{Lu}Uppercase letter
\p{N}Any number
\p{P}Any punctuation
\p{S}Any symbol
\p{Z}Any separator
\p{Emoji}Any emoji
\P{...}Negation (non-matching)

Character Class Syntax

Inside [...]:

SyntaxMeaning
[abc]a, b, or c
[a-z]a through z
[^abc]Not a, b, or c
[a-zA-Z]Any letter
[\d\s]Digit or whitespace
[.]Literal dot (no escape needed)
[-]Literal hyphen (at start/end)
[\]]Literal bracket (escaped)

Operator Precedence

From highest to lowest:

  1. \ — Escape
  2. (), (?:), (?=), etc. — Grouping
  3. *, +, ?, {n,m} — Quantifiers
  4. ^, $, \b — Anchors
  5. | — Alternation

Example:

ab|cd   = (ab)|(cd)     — alternation has lowest precedence
ab+     = a(b+)         — quantifier binds to preceding char
^ab$    = (^)(ab)($)    — anchors are separate

Quick Examples

PatternMatches
^\d+$String of only digits
^[a-z]+$String of only lowercase
\b\w+\bWhole words
"[^"]*"Quoted strings
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}IP address (basic)
(?=.*[A-Z])(?=.*\d).{8,}Password with uppercase and digit