← All Articles ยท ยท 3 min read

Regex Cheat Sheet: The Only Regex Reference You'll Need in 2026

Complete regex cheat sheet with practical examples. Covers character classes, quantifiers, anchors, groups, lookaheads, and common patterns for JavaScript, Python, and Go.

regexcheatsheetjavascriptpythonreference

Regex Cheat Sheet: The Only Regex Reference Youโ€™ll Need in 2026

Character Classes

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

Quantifiers

PatternMeaning
*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n
{n,}n or more
{n,m}Between n and m
*? / +?Lazy (minimum) match

Anchors

PatternMeaning
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary

Common JavaScript Patterns

// Email
/^[^\s@]+@[^\s@]+\.[^\s@]+$/

// URL
/https?:\/\/[^\s]+$/

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

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

// Date YYYY-MM-DD
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/

Lookahead and Lookbehind

// Positive lookahead: X followed by Y
/(?=Y)X/

// Negative lookahead: X not followed by Y
/(?!Y)X/

// Positive lookbehind: X preceded by Y
/(?<=Y)X/

// Negative lookbehind: X not preceded by Y
/(?<!Y)X/

// Example: match "price" not preceded by "$"
/(?<!\$)\d+(\.\d{2})?/  // captures "49.99" but not "$49.99"

Flags

FlagMeaning
gGlobal โ€” find all matches
iCase insensitive
mMultiline โ€” ^ and $ match line boundaries
sDotall โ€” . matches newlines
uUnicode

Python Example

import re

# Find all emails
text = "Contact alice@example.com or bob@test.org"
emails = re.findall(r'[\w.+-]+@[\w.-]+', text)

# Validate and extract
pattern = r'^(\d{4})-(0[1-9]|1[0-2])-([0-9]{2})$'
match = re.match(pattern, "2026-03-24")
if match:
    year, month, day = match.groups()

Common Mistakes

MistakeCorrect Pattern
Using .+ when .+? is needed.+ is greedy โ€” may over-match
Forgetting ^ and $ anchorsInput might contain extra text around the match
Using \d in [a-z] ranges\d is digits only, not the same as 0-9 in character classes

Testing Your Regex

Always test regex in isolation before using in production. Use a regex tester that shows:

  • All matches highlighted
  • Capture groups separated
  • Step-by-step match explanation

Free Newsletter

Level Up Your Dev Workflow

Get new tools, guides, and productivity tips delivered to your inbox.

Plus: grab the free Developer Productivity Checklist when you subscribe.

Found this guide useful? Check out our free developer tools.