env.dev

Regex Cheat Sheet for Developers

A quick-reference guide to regular expression syntax: patterns, quantifiers, groups, and examples.

A quick reference for regular expression syntax. All examples use JavaScript-compatible regex.

Anchors

PatternDescriptionExample
^Start of string (or line in multiline mode)^Hello → matches "Hello world"
$End of string (or line in multiline mode)world$ → matches "Hello world"
\bWord boundary\bcat\b → "cat" but not "catch"
\BNon-word boundary\Bcat → matches "scat"

Character Classes

PatternMatches
.Any character except newline
\dDigit [0-9]
\DNon-digit
\wWord character [a-zA-Z0-9_]
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any of: a, b, c
[^abc]Any character except a, b, c
[a-z]Any lowercase letter
[a-zA-Z0-9]Any letter or digit

Quantifiers

PatternMatchesGreedy?
*Zero or moreYes
+One or moreYes
?Zero or oneYes
{n}Exactly n times
{n,}n or more timesYes
{n,m}Between n and m timesYes
*?Zero or more (lazy)No
+?One or more (lazy)No

Groups & Alternation

PatternDescription
(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
a|ba or b
\1Backreference to group 1
\k<name>Backreference to named group

Lookahead & Lookbehind

PatternNameDescription
(?=abc)Positive lookaheadFollowed by abc
(?!abc)Negative lookaheadNot followed by abc
(?<=abc)Positive lookbehindPreceded by abc
(?<!abc)Negative lookbehindNot preceded by abc

Flags (JavaScript)

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitiveIgnore case
mMultiline^ and $ match start/end of each line
sDotall. matches newline characters too
uUnicodeEnable full Unicode matching
dIndicesPopulate match.indices with group positions

Common Patterns

Email
^[^\s@]+@[^\s@]+\.[^\s@]+$
URL
https?://[^\s/$.?#].[^\s]*
IPv4
\b(?:\d{1,3}\.){3}\d{1,3}\b
Hex color
#(?:[0-9a-fA-F]{3}){1,2}\b
Date (YYYY-MM-DD)
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Slug
^[a-z0-9]+(?:-[a-z0-9]+)*$
UUID v4
[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}
JWT
^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$

Test your regex patterns in real time with the Regex Tester tool.