Regular expressions are one of those tools that every developer encounters constantly but few fully master. The syntax looks cryptic at first but once you understand the patterns it becomes an incredibly powerful skill. This cheat sheet is designed to be your go-to quick reference -covering every major regex concept with practical examples you can test and use immediately. Bookmark this page and come back whenever you need to look something up.
Character Classes -Matching Specific Characters
- `.` -Matches any single character except a newline. Example: `c.t` matches `cat`, `cut`, `cot`
- `\d` -Matches any digit 0-9. Equivalent to `[0-9]`. Example: `\d+` matches `123` in `abc123`
- `\D` -Matches any character that is NOT a digit. Example: `\D+` matches `abc` in `abc123`
- `\w` -Matches word characters: letters, digits, and underscore. Equivalent to `[a-zA-Z0-9_]`
- `\W` -Matches any non-word character -spaces, punctuation, symbols
- `\s` -Matches whitespace characters: space, tab, newline, carriage return
- `\S` -Matches any non-whitespace character
- `[abc]` -Character class matching any one of a, b, or c
- `[^abc]` -Negated character class matching anything except a, b, or c
- `[a-z]` -Range matching any lowercase letter. Combine as `[a-zA-Z0-9]`
Quantifiers -How Many Times to Match
- `*` -Zero or more times (greedy). `ab*` matches `a`, `ab`, `abb`, `abbb`
- `+` -One or more times (greedy). `ab+` matches `ab`, `abb` but NOT `a`
- `?` -Zero or one time -makes something optional. `colou?r` matches both `color` and `colour`
- `{n}` -Exactly n times. `\d{4}` matches exactly 4 digits
- `{n,}` -At least n times. `\d{2,}` matches 2 or more digits
- `{n,m}` -Between n and m times. `\d{2,4}` matches 2, 3, or 4 digits
- `*?` -Zero or more times (lazy -matches as few as possible). Use to avoid over-matching
- `+?` -One or more times (lazy). `<.+?>` matches individual HTML tags instead of everything between first and last tag`
Greedy quantifiers match as much as possible. Lazy quantifiers (add ?) match as little as possible. When your regex matches too much, try making quantifiers lazy.
Anchors -Matching Positions
- `^` -Matches the start of the string. With `m` flag matches start of each line
- `$` -Matches the end of the string. With `m` flag matches end of each line
- `\b` -Word boundary -the position between a word character and a non-word character. `\bcat\b` matches `cat` but not `catch` or `scat`
- `\B` -Non-word boundary -matches where `\b` does not
Groups -Capturing and Organizing
- `(pattern)` -Capturing group. The matched text is available as `$1`, `$2` etc. in replacements and as numbered groups in match results
- `(?:pattern)` -Non-capturing group. Groups without capturing -useful for applying quantifiers without creating a group
- `(?<name>pattern)` -Named capturing group. Reference by name as `$<name>` in replacements
- `(a|b)` -Alternation inside a group -matches a or b
- `(?=pattern)` -Positive lookahead -matches if followed by pattern, but does not consume characters
- `(?!pattern)` -Negative lookahead -matches if NOT followed by pattern
- `(?<=pattern)` -Positive lookbehind -matches if preceded by pattern
- `(?<!pattern)` -Negative lookbehind -matches if NOT preceded by pattern
Common Regex Patterns You Can Use Today
- Email: `[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}`
- URL: `https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b`
- Phone (Indian mobile): `(\+91[\-\s]?)?[0]?(91)?[6789]\d{9}`
- IPv4 address: `\b(?:(?: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]?)\b`
- Date DD/MM/YYYY: `(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d`
- Hex color: `#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\b`
- Slug (URL-friendly): `^[a-z0-9]+(?:-[a-z0-9]+)*$`
- Strong password: `^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$`
Regex Flags Reference
- `g` (global) -Find all matches. Without this only the first match is returned
- `i` (case insensitive) -Match regardless of case. `/hello/i` matches Hello, HELLO, hello
- `m` (multiline) -Makes `^` and `$` match start and end of each line
- `s` (dot all) -Makes `.` match newlines too. Without this `.` skips `\n`
- `u` (unicode) -Enables Unicode matching and `\u{XXXXXX}` escape sequences
- `d` (indices) -Includes start and end indices for each match and group in results
Avoid catastrophic backtracking by not using nested quantifiers on overlapping patterns (like `(a+)+`). These can cause your regex engine to run for exponentially longer as the string grows.