Published March 27, 2026 · 15 min read

Regex Cheat Sheet 2026: Every Pattern With Real Examples

Regular expressions are one of the most powerful tools in programming, and also one of the most confusing. This cheat sheet covers every regex pattern you will actually use in practice, with real examples you can test immediately in our free Regex Tester.

Bookmark this page. You will come back to it.

Table of Contents

  1. Basic Characters
  2. Character Classes
  3. Quantifiers
  4. Anchors and Boundaries
  5. Groups and Capturing
  6. Lookahead and Lookbehind
  7. Flags / Modifiers
  8. Common Real-World Patterns
  9. Regex in JavaScript
  10. Performance Tips

Basic Characters

PatternDescriptionExampleMatches
.Any character except newlineh.that, hot, hit
\Escape special character\.literal dot
|OR operatorcat|dogcat or dog

The dot is the most common beginner pitfall. It matches almost everything, which means it often matches more than you intended. When you want a literal dot (like in a domain name), always escape it: \.

Pattern: https?://[\w.-]+\.\w+
Input: Visit https://spunk.codes for free tools
Match: https://spunk.codes

Character Classes

PatternDescriptionEquivalent
[abc]Any one of a, b, or ca|b|c
[^abc]Any character NOT a, b, or c
[a-z]Any lowercase letter
[A-Z]Any uppercase letter
[0-9]Any digit\d
\dAny digit[0-9]
\DAny non-digit[^0-9]
\wWord character[a-zA-Z0-9_]
\WNon-word character[^a-zA-Z0-9_]
\sWhitespace (space, tab, newline)[ \t\n\r\f\v]
\SNon-whitespace[^ \t\n\r\f\v]

Pro tip: \w includes the underscore character. This is why it works well for matching variable names in most programming languages, but it will not match hyphens in CSS class names. Use [\w-] for CSS selectors.

Quantifiers

PatternDescriptionExampleMatches
*0 or moreab*cac, abc, abbc, abbbc
+1 or moreab+cabc, abbc, abbbc (not ac)
?0 or 1colou?rcolor, colour
{n}Exactly n times\d{4}1234, 5678
{n,}n or more times\d{2,}12, 123, 1234
{n,m}Between n and m times\d{2,4}12, 123, 1234
*?0 or more (lazy)<.*?>matches shortest tag
+?1 or more (lazy)".+?"matches shortest quoted string

Greedy vs Lazy: The Most Common Bug

By default, quantifiers are greedy — they match as much as possible. This causes problems when parsing HTML or extracting quoted strings.

Greedy: <.+>
Input: <div>Hello</div>
Match: <div>Hello</div> (entire string)
Lazy: <.+?>
Input: <div>Hello</div>
Matches: <div> and </div> (two separate matches)

Anchors and Boundaries

PatternDescriptionExample
^Start of string (or line with m flag)^Hello
$End of string (or line with m flag)world$
\bWord boundary\bcat\b matches "cat" not "cats"
\BNon-word boundary\Bcat\B matches "concatenate"

Word boundaries are incredibly useful for searching code. \bfunction\b matches the keyword "function" without matching "malfunction" or "dysfunction." Always use \b when searching for exact words.

Groups and Capturing

PatternDescriptionExample
(abc)Capturing group(\d{3})-(\d{4})
(?:abc)Non-capturing group(?:https?|ftp)://
(?<name>abc)Named capturing group(?<year>\d{4})
\1Backreference to group 1(\w+)\s\1 matches repeated words
Named groups: (?<month>\d{2})/(?<day>\d{2})/(?<year>\d{4})
Input: 03/27/2026
Groups: month=03, day=27, year=2026

Named groups make your regex self-documenting. Instead of remembering that group 1 is the month and group 2 is the day, you access them by name. Every modern regex engine supports this syntax.

Lookahead and Lookbehind

PatternDescriptionExample
(?=abc)Positive lookahead\d+(?=px) matches digits before "px"
(?!abc)Negative lookahead\d+(?!px) matches digits NOT before "px"
(?<=abc)Positive lookbehind(?<=\$)\d+ matches digits after "$"
(?<!abc)Negative lookbehind(?<!\$)\d+ matches digits NOT after "$"

Lookarounds are zero-width assertions. They check if something exists before or after your match without including it in the result. This is essential for extracting data from structured text.

Extract price numbers: (?<=\$)\d+\.?\d*
Input: The price is $49.99 per month
Match: 49.99 (without the dollar sign)

Flags / Modifiers

FlagDescriptionUsage
gGlobal — find all matches, not just first/pattern/g
iCase insensitive/hello/i matches Hello, HELLO
mMultiline — ^ and $ match line start/end/^start/m
sDotall — dot matches newlines too/start.*end/s
uUnicode — proper Unicode character handling/\p{Emoji}/u

Common Real-World Patterns

Email Validation (Practical)

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

This is not RFC 5322 compliant (that regex is 6,000+ characters), but it validates 99.9% of real email addresses correctly.

URL Matching

https?:\/\/[\w\-._~:/?#\[\]@!$&'()*+,;=%]+

Phone Number (US)

^(\+1)?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

Matches: 555-123-4567, (555) 123-4567, +1 555.123.4567, 5551234567

IPv4 Address

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$

Hex Color Code

^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$

Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Requires: 8+ characters, one uppercase, one lowercase, one digit, one special character.

HTML Tag Extraction

<(\w+)(?:\s[^>]*)?\/?>

CSS Property-Value

([\w-]+)\s*:\s*([^;]+);

Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Using Regex in JavaScript

// Test if a string matches
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
emailRegex.test('user@example.com'); // true

// Extract matches
const text = 'Price: $49.99 and $29.99';
const prices = text.match(/(?<=\$)\d+\.?\d*/g);
// prices = ['49.99', '29.99']

// Named groups with exec
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const result = dateRegex.exec('2026-03-27');
// result.groups.year = '2026'

// Replace with pattern
'hello world'.replace(/(\w+)\s(\w+)/, '$2 $1');
// 'world hello'

// Replace with function
'abc'.replace(/[a-c]/g, match => match.toUpperCase());
// 'ABC'

Performance Tips

  1. Avoid catastrophic backtracking. Patterns like (a+)+ can take exponential time on certain inputs. Use atomic groups or possessive quantifiers when available.
  2. Be specific. [0-9] is faster than . when you know you are matching digits. Specificity reduces the regex engine's work.
  3. Anchor when possible. ^pattern is faster than pattern alone because the engine does not need to try matching at every position.
  4. Use non-capturing groups. (?:abc) is slightly faster than (abc) when you do not need the captured value.
  5. Compile once, use many times. In JavaScript, define your regex outside the loop: const re = /pattern/g;

Test Your Regex Patterns Instantly

Our free regex tester shows matches in real time with highlighted groups, flags, and explanations.

Open Regex Tester All 650+ Free Tools

Related Tools