Regex Cheatsheet for Developers (with Real Examples)
Regular expressions are intimidating but enormously powerful. This cheatsheet covers the patterns you will use 90% of the time.
Farhan Murtaza is the founder of Toolsfluent and a full-stack web developer with four years of professional experience building production websites in Next.js, TypeScript, PHP, and WordPress. He has worked on enterprise WooCommerce sites, custom WordPress plugins, and modern React applications. He builds Toolsfluent as a curated, privacy-first hub of utilities for developers, students, freelancers, and small business owners worldwide.
Regular expressions (regex) let you match patterns in text. They look cryptic at first but follow consistent rules. This cheatsheet covers the most common patterns.
Character classes
- `\d` matches any digit (0-9) - `\w` matches any word character (letters, digits, underscore) - `\s` matches any whitespace (space, tab, newline) - `.` matches any character except newline - `[abc]` matches any of a, b, c - `[^abc]` matches anything except a, b, c
Quantifiers
- `*` zero or more - `+` one or more - `?` zero or one (optional) - `{n}` exactly n times - `{n,m}` between n and m times
Anchors
- `^` start of string - `$` end of string - `\b` word boundary
Common patterns
**Email (simple):** `/^[\w.-]+@[\w-]+\.[\w.-]+$/` **URL:** `/^https?:\/\/[\w.-]+(\/[\w./?=#-]*)?$/` **Phone (US):** `/^\+?1?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/` **Hex color:** `/^#?([a-f\d]{6}|[a-f\d]{3})$/i` **Date YYYY-MM-DD:** `/^\d{4}-\d{2}-\d{2}$/`
Capture groups
Wrap part of the pattern in parentheses to capture it. `(\d{4})-(\d{2})-(\d{2})` captures year, month, day separately.
Use named groups for clarity: `(?
Lookahead and lookbehind
Lookahead `(?=...)` matches if followed by, but does not consume the match. Lookbehind `(?<=...)` matches if preceded by.
Test before deploying
Always test regex against many inputs, including edge cases (empty string, special characters, very long input). Browse our JSON Formatter for related developer utilities.
