BuildUtilities

Regex Cheat Sheet

Everything you'll reach for when writing or reading a regex. JavaScript syntax, other flavours are similar but check your engine's docs for lookbehind, named groups, and Unicode rules.

Character classes

.Any character except newline
\dDigit (0–9)
\DNon-digit
\wWord character (a–z, A–Z, 0–9, _)
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Anything except a, b, or c
[a-z]Range (lowercase letters)

Anchors

^Start of string (or line, with /m)
$End of string (or line, with /m)
\bWord boundary
\BNon-word boundary

Quantifiers

*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n
{n,}n or more
{n,m}Between n and m
*? +? ??Lazy (non-greedy) versions, match as little as possible

Groups & alternation

(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
a|bEither a or b
\1, \2…Backreference to capturing group

Lookarounds

(?=abc)Lookahead, followed by abc
(?!abc)Negative lookahead. NOT followed by abc
(?<=abc)Lookbehind, preceded by abc
(?<!abc)Negative lookbehind. NOT preceded by abc

Flags

gGlobal, find all matches, not just the first
iCase-insensitive
mMultiline, ^ and $ match line breaks
sDotall, . matches newlines too
uUnicode mode
ySticky, match must start at lastIndex

Related tools

More references

Tip Jar