Cheatsheet
Regex Cheat Sheet – Regular Expressions Reference
Complete regular expressions cheat sheet with character classes, quantifiers, anchors, groups, and real-world patterns. Copy any pattern with one click.
Anchors
^Start of string (or line with m flag) $End of string (or line with m flag) \bWord boundary (between \w and \W) \BNot a word boundary Character Classes
\dAny digit [0-9] \DAny non-digit \wWord character [a-zA-Z0-9_] \WNon-word character \sWhitespace (space, tab, newline) \SNon-whitespace [abc]Match a, b, or c [^abc]Any character except a, b, c [a-z]Any lowercase letter a through z .Any character except newline (use s flag for newline too) Quantifiers
*0 or more (greedy) +1 or more (greedy) ?0 or 1 (optional) {n}Exactly n times {n,}n or more times {n,m}Between n and m times *?0 or more (lazy — match as few as possible) +?1 or more (lazy) Groups & Lookaheads
(...)Capturing group (?:...)Non-capturing group (?<name>...)Named capturing group (?=...)Positive lookahead — followed by (?!...)Negative lookahead — NOT followed by (?<=...)Positive lookbehind — preceded by (?<!...)Negative lookbehind — NOT preceded by a|bMatch a or b (alternation) Flags
gGlobal — find all matches, not just the first iCase-insensitive matching mMultiline — ^ and $ match line start/end sDotall — . matches newline characters too uUnicode — enables full Unicode support Common Patterns
^[\w.-]+@[\w.-]+\.\w{2,}$Email address https?:\/\/[\w.-]+(?:\/[\w./?%&=-]*)?URL (http/https) ^\+?[\d\s\-().]{7,15}$Phone number (international) ^\d{4}-\d{2}-\d{2}$Date (YYYY-MM-DD) ^(?:\d{1,3}\.){3}\d{1,3}$IPv4 address ^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$Hex color (#fff or #ffffff) ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$Strong password (8+ chars, upper, lower, digit, symbol) ^\d{5}(?:-\d{4})?$US ZIP code (12345 or 12345-6789) What is a Regular Expression?
A regular expression (regex) is a pattern used to match, search, or replace text. They're supported natively in JavaScript, Python, PHP, Ruby, Java, and virtually every modern language. Mastering regex saves hours of string-manipulation code.
How to Test Your Regex
Use an online tester like regex101.com to build and debug patterns interactively with real-time match highlighting.
Regex Performance Tips
- Avoid catastrophic backtracking by not nesting quantifiers:
(.+)+can freeze a browser - Use non-capturing groups
(?:...)when you don't need the captured value - Anchor your patterns with
^and$to prevent partial matches when doing validation