Regex Cheat Sheet
Quick reference for regular expressions. Test patterns with our Regex Tester.
Character Classes
| . | Any character except newline |
| \w | Word character [a-zA-Z0-9_] |
| \W | Non-word character |
| \d | Digit [0-9] |
| \D | Non-digit |
| \s | Whitespace (space, tab, newline) |
| \S | Non-whitespace |
| [abc] | Any of a, b, or c |
| [^abc] | Not a, b, or c |
| [a-z] | Range: a to z |
Anchors
| ^ | Start of string (or line with m flag) |
| $ | End of string (or line with m flag) |
| \b | Word boundary |
| \B | Non-word boundary |
Quantifiers
| * | 0 or more (greedy) |
| + | 1 or more (greedy) |
| ? | 0 or 1 (optional) |
| {3} | Exactly 3 |
| {3,} | 3 or more |
| {3,5} | Between 3 and 5 |
| *? | 0 or more (lazy) |
| +? | 1 or more (lazy) |
Groups & Lookaround
| (abc) | Capturing group |
| (?:abc) | Non-capturing group |
| (?<name>abc) | Named capturing group |
| \1 | Backreference to group 1 |
| (?=abc) | Positive lookahead |
| (?!abc) | Negative lookahead |
| (?<=abc) | Positive lookbehind |
| (?<!abc) | Negative lookbehind |
Flags
| g | Global — match all occurrences |
| i | Case-insensitive |
| m | Multiline — ^ and $ match line start/end |
| s | Dotall — . matches newline |
| u | Unicode support |
Common Patterns
| ^[\w.-]+@[\w.-]+\.\w{2,}$ | Email (simple) |
| ^https?://[^\s]+$ | URL |
| ^\d{4}-\d{2}-\d{2}$ | Date (YYYY-MM-DD) |
| ^\+?\d{10,15}$ | Phone number |
| ^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ | Hex color |
| ^(?=.*[A-Z])(?=.*\d).{8,}$ | Strong password |