Regex Cheatsheet
A quick reference for regular expression syntax — anchors, character classes, quantifiers, groups, lookaheads, flags, and common real-world patterns.
Try the Regex TesterSections
Anchors
| Token | Description |
|---|---|
^ | Start of string (or line in multiline mode) |
$ | End of string (or line in multiline mode) |
\b | Word boundary |
\B | Not a word boundary |
\A | Start of string (no multiline) |
\Z | End of string (no multiline) |
Character Classes
| Token | Description |
|---|---|
. | Any character except newline |
\w | Word character: [a-zA-Z0-9_] |
\W | Non-word character |
\d | Digit: [0-9] |
\D | Non-digit |
\s | Whitespace (space, tab, newline, etc.) |
\S | Non-whitespace |
[abc] | Character set — matches a, b, or c |
[^abc] | Negated character set — matches anything except a, b, or c |
[a-z] | Character range — matches any lowercase letter |
[a-zA-Z0-9] | Multiple ranges — alphanumeric |
Quantifiers
| Token | Description |
|---|---|
* | 0 or more (greedy) |
+ | 1 or more (greedy) |
? | 0 or 1 (greedy) |
{n} | Exactly n times |
{n,} | n or more times |
{n,m} | Between n and m times |
*? | 0 or more (lazy — matches as few as possible) |
+? | 1 or more (lazy) |
?? | 0 or 1 (lazy) |
Groups & Alternation
| Token | Description |
|---|---|
(abc) | Capturing group — captures match as group 1, 2, … |
(?:abc) | Non-capturing group — groups without capturing |
(?<name>abc) | Named capturing group — accessible as group 'name' |
a|b | Alternation — matches a or b |
\1 | Backreference to group 1 |
\k<name> | Backreference to named group 'name' |
Lookahead & Lookbehind
| Token | Description |
|---|---|
(?=abc) | Positive lookahead — asserts what follows matches abc |
(?!abc) | Negative lookahead — asserts what follows does NOT match abc |
(?<=abc) | Positive lookbehind — asserts what precedes matches abc |
(?<!abc) | Negative lookbehind — asserts what precedes does NOT match abc |
Escapes & Special Characters
| Token | Description |
|---|---|
\n | Newline |
\t | Tab |
\r | Carriage return |
\0 | Null character |
\uFFFF | Unicode character by code point |
\. | Literal dot (escape reserved characters with \) |
Flags
| Flag | Description |
|---|---|
g | Global — find all matches, not just the first |
i | Case-insensitive — treats A and a as equivalent |
m | Multiline — ^ and $ match start/end of each line |
s | Dot-all (dotAll) — makes . match newline characters too |
u | Unicode — enables full Unicode matching and escape sequences |
y | Sticky — match only at the current position in the string |
Common Patterns
| Use Case | Pattern |
|---|---|
Email address | [a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} |
URL (http/https) | https?:\/\/[^\s/$.?#].[^\s]* |
IPv4 address | \b(?:\d{1,3}\.){3}\d{1,3}\b |
Hex colour code | #(?:[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]) |
Integer (positive) | \b[1-9]\d*\b |
Decimal number | -?\d+(?:\.\d+)? |
Slug (URL-safe) | [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 token | [A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+ |
HTML tag | <([a-z][a-z0-9]*)(?:\s[^>]*)?> |
Whitespace (trim) | ^\s+|\s+$ |
How to Use Regular Expressions
A practical guide to regular expressions — learn the core syntax, common patterns, and how to write regex for real-world tasks like email validation, URL matching, and log parsing.
Read guide →How to Write a Regex Pattern Step by Step
A practical guide to building regular expressions from scratch — breaking down the problem, choosing the right tokens, testing incrementally, and avoiding common traps.
Read guide →