Skip to content

Regex Cheat Sheet

Regular expression syntax and flags, with examples.

About Regex cheat sheet

Regular expressions describe text patterns with a compact syntax: classes say which characters can appear, quantifiers say how many times, anchors say where, and groups capture parts of a match or combine alternatives. The same core syntax works in JavaScript, Python, Java, Go, PCRE (PHP, many editors) and grep with -E, with differences in advanced features.

This sheet follows JavaScript (ECMAScript), the flavor the regex tester uses. Notable differences elsewhere: Python spells named groups (?P<name>...), Go's RE2 has no lookaround or backreferences by design, and POSIX tools use classes like [[:digit:]] instead of \d. Try any pattern on your own text in the regex tester before you rely on it.

54 entries

Characters and classes

SyntaxMeaningExample
.Any character except a line break (any character with the s flag)a.c matches abc, a-c
\dA digit, 0-9\d\d matches 42
\DNot a digit
\wA word character: A-Z, a-z, 0-9 or _\w+ matches user_1
\WNot a word character
\sWhitespace: space, tab, line breaks and other Unicode spaces
\SNot whitespace
[abc]One of a, b or cgr[ae]y matches gray and grey
[^abc]Any character except a, b or c
[a-z]A character in the range a to z[A-Fa-f0-9] matches a hex digit
\t \n \rTab, line feed, carriage return
\uFFFFA character by its UTF-16 code unit\u00e9 matches é
\u{1F600}A code point (needs the u flag)
\p{L}A Unicode property: any letter (needs the u flag)\p{Lu} uppercase, \p{N} number
\.A literal dot: escape . * + ? ^ $ ( ) [ ] { } | \ / with a backslash\.com matches .com

Anchors and boundaries

SyntaxMeaningExample
^Start of the string (start of each line with the m flag)^Hello
$End of the string (end of each line with the m flag)world$
\bA word boundary\bcat\b matches cat but not concat
\BNot a word boundary

Quantifiers

SyntaxMeaningExample
*0 or moreab*c matches ac, abc, abbc
+1 or moreab+c matches abc, abbc
?0 or 1 (optional)colou?r matches color and colour
{3}Exactly 3\d{3} matches 123
{2,}2 or more
{2,5}Between 2 and 5
*? +? ??Lazy: as few as possible<.+?> matches one tag at a time

Groups, alternation and lookaround

SyntaxMeaningExample
(abc)Capturing group(\d+)-(\d+) captures both numbers
(?<name>abc)Named capturing group(?<year>\d{4})
(?:abc)Group without capturing(?:https?://)?
a|ba or bcat|dog
\1Backreference to group 1(\w)\1 matches a doubled letter
\k<name>Backreference to a named group
(?=abc)Lookahead: followed by abc\d+(?=px) matches 12 in 12px
(?!abc)Negative lookahead: not followed by abc
(?<=abc)Lookbehind: preceded by abc(?<=\$)\d+ matches 5 in $5
(?<!abc)Negative lookbehind: not preceded by abc

JavaScript flags

SyntaxMeaningExample
gGlobal: find all matches, not just the first/a/g
iIgnore case/hello/i matches HELLO
mMultiline: ^ and $ match at line breaks
sDot-all: . also matches line breaks
uUnicode: code points and \p{...} escapes
vUnicode sets: u plus set operations in classes (ES2024)
ySticky: match only at lastIndex
dIndices: report start and end of each group

Handy patterns

Starting points, not complete validators: real-world formats have edge cases, so test with your own data.

SyntaxMeaningExample
^\d+$Whole string is digits12345
^-?\d+(\.\d+)?$Integer or decimal number-3.14
^[^\s@]+@[^\s@]+\.[^\s@]+$Rough email shape (something@something.tld)ada@example.com
^https?://\S+$A URL starting with http:// or https://
^\d{4}-\d{2}-\d{2}$Date shaped like YYYY-MM-DD (doesn't check the date exists)2026-10-02
^#?([0-9a-f]{3}|[0-9a-f]{6})$Hex color, with the i flag#33aabb
^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$UUID, with the i flag
^\s+|\s+$Leading or trailing whitespace (use with g to trim)
\s{2,}Runs of two or more whitespace characters
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$At least 8 characters with a digit, a lowercase and an uppercase letter

Good to know

Avoid catastrophic backtracking

Nested quantifiers such as (a+)+ or (.*)*, or alternatives that can match the same text, can take exponential time on input that almost matches. On a server that's a denial-of-service risk (ReDoS). Make each part of the pattern match as specifically as possible.

More references