Regex
Regular expression quick reference: character classes, anchors, quantifiers, groups, lookahead/lookbehind, flags, and common patterns.
A regular expression (regex) is a pattern used to match text. This reference covers the syntax common to JavaScript, Python, and PCRE. Differences between flavors are noted where they exist.
Character Classes
| Pattern | Matches |
|---|---|
| . | Any character except newline (use /s flag to include newline) |
| \d | Digit [0-9] |
| \D | Non-digit |
| \w | Word character [a-zA-Z0-9_] |
| \W | Non-word character |
| \s | Whitespace (space, tab, newline, etc.) |
| \S | Non-whitespace |
| [abc] | Character set — matches a, b, or c |
| [^abc] | Negated set — matches anything except a, b, or c |
| [a-z] | Range — matches any lowercase letter |
| [a-zA-Z] | Range — any letter (case insensitive set) |
Anchors & Boundaries
^Start of string (or line in multiline mode)$End of string (or line in multiline mode)\bWord boundary — between \w and \W\BNon-word boundary\AStart of string (Python/PCRE — not affected by multiline)\ZEnd of string (Python/PCRE)Quantifiers
| Quantifier | Meaning | Lazy version |
|---|---|---|
| * | 0 or more | *? |
| + | 1 or more | +? |
| ? | 0 or 1 | ?? |
| {n} | Exactly n times | {n}? |
| {n,} | n or more times | {n,}? |
| {n,m} | Between n and m times (inclusive) | {n,m}? |
Greedy quantifiers match as much as possible. Lazy (? suffix) match as little as possible.
Groups & References
(abc)Capturing group — stores match in $1 / \1(?:abc)Non-capturing group — groups without storing(?<name>abc)Named capturing group — access as $<name> or \k<name>\1Backreference to group 1 (within the pattern)$1Group 1 in replacement stringsa|bAlternation — matches a or bLookahead & Lookbehind
(?=...)Positive lookahead
Matches position followed by the pattern. E.g. \d+(?= dollars) matches a number before " dollars".
(?!...)Negative lookahead
Matches position NOT followed by the pattern.
(?<=...)Positive lookbehind
Matches position preceded by the pattern.
(?<!...)Negative lookbehind
Matches position NOT preceded by the pattern.
Lookbehind requires fixed-width patterns in most engines. Variable-width lookbehind is supported in JS (v8), Python 3.11+, and PCRE2.
Flags / Modifiers
| Flag | Name | Effect | JS | Python |
|---|---|---|---|---|
| i | Case insensitive | Matches upper and lowercase as equal | re.I | |
| g | Global | Find all matches, not just the first | findall() | |
| m | Multiline | ^ and $ match line start/end (not just string) | re.M | |
| s | Dot all | . matches newline characters too | re.S | |
| u | Unicode | Enables full Unicode matching | default | |
| x | Verbose | Allows whitespace and comments inside the regex | re.X | |
| d | Indices | Provides indices for match start/end | ES2022 |
Common Patterns
| Use case | Pattern |
|---|---|
| Email address (basic) | /^[^\s@]+@[^\s@]+\.[^\s@]+$/i |
| URL (http/https) | /https?:\/\/[^\s]+/ |
| IPv4 address | /^(\d{1,3}\.){3}\d{1,3}$/ |
| Phone (US, flexible) | /^[+]?[1]?[\s-]?\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/ |
| Date YYYY-MM-DD | /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/ |
| Hex color | /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/ |
| UUID v4 | /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i |
| Slug (URL-friendly) | /^[a-z0-9]+(?:-[a-z0-9]+)*$/ |
| Integer (positive/negative) | /^-?\d+$/ |
| Float | /^-?\d+(\.\d+)?$/ |
| Whitespace-only string | /^\s*$/ |
| Strip HTML tags | /<[^>]*>/g |
Test your regex with our Regex Tester →
Real-time matching with capture group highlighting.