Complete regex cheat sheet for 2026: anchors, quantifiers, groups, lookaheads, and common patterns for JavaScript, Python, and more. Test regex live in your browser.
Regular expressions are the most transferable skill in programming. The same pattern syntax works in JavaScript, Python, Go, Java, Ruby, PHP, bash, sed, grep, and dozens of other languages and tools. Learning regex once gives you a superpower that works everywhere.
This guide covers the complete regex syntax with examples, common patterns you can use immediately, and how to use our live regex tester to build and debug your own patterns. Bookmark this page — it is the reference I keep open every day.
Try it yourself: Free Regex Tester — free, no signup, runs in your browser.
Regex Syntax Quick Reference
Anchors
^— Start of string (or start of line in multiline mode)$— End of string (or end of line in multiline mode)— Word boundary (between a word character and a non-word character)B— Not a word boundaryA— Start of string (Python; not supported in JS)Z— End of string (Python; not supported in JS)
Character Classes
.— Any character except newline (use[sS]to include newlines)d— Digit (0-9)D— Not a digitw— Word character (a-z, A-Z, 0-9, underscore)W— Not a word characters— Whitespace (space, tab, newline, carriage return)S— Not whitespace[abc]— Any one of a, b, or c[^abc]— Any character except a, b, or c[a-z]— Any character in range a through z[a-zA-Z0-9]— Alphanumeric
Quantifiers
*— Zero or more (greedy)+— One or more (greedy)?— Zero or one (greedy){n}— Exactly n times{n,}— n or more times{n,m}— Between n and m times (inclusive)*?— Zero or more (lazy — matches as few as possible)+?— One or more (lazy)??— Zero or one (lazy)
Greedy vs. lazy matters. The pattern <.+> applied to <div>Hello</div> matches the entire string (greedy, matches as much as possible). The pattern <.+?> matches only <div> (lazy, matches as little as possible). For HTML parsing, lazy quantifiers almost always give the intended result.
Groups and References
(abc)— Capturing group(?:abc)— Non-capturing group (groups without creating a backreference)(?<name>abc)— Named capturing group\1— Backreference to group 1\k<name>— Backreference to named group
Lookaheads and Lookbehinds
(?=abc)— Positive lookahead (followed by abc)(?!abc)— Negative lookahead (not followed by abc)(?<=abc)— Positive lookbehind (preceded by abc)(?<!abc)— Negative lookbehind (not preceded by abc)
Lookaheads and lookbehinds are zero-width assertions — they match a position but do not consume characters. They are essential for patterns like "find all prices" ((?<=$)d+.d{2}) or "find all words not followed by a comma" (w+(?!,)).
Flags
g— Global (find all matches, not just the first)i— Case insensitivem— Multiline (^and$match start/end of each line)s— Dotall (. matches newlines)u— Unicode mode (treat pattern as Unicode code points)x— Extended/verbose mode (allows whitespace and comments in pattern) — Python, not JS
Common Regex Patterns (Copy-Paste Ready)
Validation Patterns
Email address (reasonable approximation):
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/
Indian mobile number:
/^(+91|91|0)?[6-9]d{9}$/
Indian GSTIN:
/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/
PAN card number:
/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/
URL (including http/https):
/https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}([-a-zA-Z0-9()@:%_+.~#?&=]*)/
IPv4 address:
/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
Strong password (min 8 chars, 1 uppercase, 1 lowercase, 1 digit, 1 special):
/^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{8,}$/
Extraction Patterns
Extract all numbers from a string:
/d+.?d*/g
Extract content between HTML tags:
/<tag>([sS]*?)</tag>/g
Extract all hashtags:
/#[w]+/g
Extract all mentions (@username):
/@[w]+/g
Extract key-value pairs from query string:
/([^&=?]+)=([^&]*)/g
Transformation Patterns
Convert camelCase to kebab-case:
/([a-z])([A-Z])/g → replace with $1-$2, then toLowerCase()
Convert snake_case to camelCase:
/_([a-z])/g → replace with match[1].toUpperCase()
Remove extra whitespace (collapse multiple spaces to one):
/s{2,}/g → replace with ' '
Remove HTML tags:
/<[^>]*>/g → replace with ''
Comments · 0
Beta: comments are stored locally on your device and not visible to other readers.
No comments yet. Be the first to share your thoughts.