Quick

Regex Cheat Sheet

Quick reference for the regex syntax people forget most often.

Character classes

.Any character except newline
\dDigit (0–9)
\DNon-digit
\wWord character (letter, digit, underscore)
\WNon-word character
\sWhitespace
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Anything except a, b, or c
[a-z]Any character in the range a to z
\p{L}Any Unicode letter (needs the u flag); \P{L} is anything else

Anchors

^Start of string (or line, with m flag)
$End of string (or line, with m flag)
\bWord boundary
\BNot a word boundary

Quantifiers

*0 or more
+1 or more
?0 or 1
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*? +? ??Lazy versions: match as few as possible instead of as many

Groups & alternation

(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
a|bMatch a or b
\1Backreference: the same text group 1 matched
\k<name>Backreference to a named group

Lookaround

a(?=b)a, only if followed by b (b isn't part of the match)
a(?!b)a, only if NOT followed by b
(?<=b)aa, only if preceded by b
(?<!b)aa, only if NOT preceded by b

Escapes

\. \* \( \[A literal special character: escape any of . * + ? ^ $ ( ) [ ] { } | \ /
\n \r \tNewline, carriage return, tab
\x41 \u00e9A character by its hex code

Flags

gGlobal — find all matches, not just the first
iCase-insensitive
mMultiline — ^ and $ match line boundaries
sDot matches newline too
uUnicode — treats emoji as one character and enables \p{…}
ySticky — only match exactly at the current position
Advertisement

About this tool

A quick-reference table of the regular-expression syntax supported by JavaScript's own regex engine — the same one used by every regex tool on this site. Character classes, anchors, quantifiers (including the lazy forms), groups and backreferences, lookarounds, escapes and flags are grouped together with a one-line explanation beside each, so you can look one up without leaving the page or reaching for an external cheat sheet.

Does this cover every flavour of regex?
No — it covers JavaScript's own syntax (used in the Regex Tester and Regex Find & Replace tools on this site). Python, PCRE and .NET regex differ in some details, particularly around lookbehind support and named groups.
Can I test a pattern from here?
This page is reference only. Use the Regex Tester to try a pattern against real text.