Regex Cheat Sheet: The Patterns You Actually Use
Regular expressions reward a small vocabulary used well. You do not need the whole specification on day one — you need character classes, anchors, quantifiers, and groups, plus a few patterns you will reuse for years. This cheat sheet sticks to that core, uses JavaScript-flavoured syntax throughout, and ends with ten patterns worth memorising. Test anything below in the regex tester.
Character classes and anchors
The shorthands cover most of what you match: \d for digits, \w for word characters ([A-Za-z0-9_]), and \s for whitespace. The uppercase forms negate — \D, \W, \S — so \D means "any character that is not a digit". You can also write your own class: [aeiou], a range like [a-f], or a negated class [^"] for "anything except a double quote". Inside a class the caret only negates in first position, and a hyphen at the end is literal.
Anchors pin a match to a position without consuming characters: ^ for the start of a line and $ for the end, both per line when the m flag is set. \b marks a word boundary, so \bcat\b matches the word but not the start of "concatenate". The dot . matches any character except a newline unless you pass the s flag. A classic beginner mistake is writing ^\s+ and expecting it to also strip trailing spaces — for that you need the alternation ^\s+|\s+$.
Quantifiers: greedy, lazy, and possessive
Quantifiers say how many times something may repeat: * for zero or more, + for one or more, ? for optional, and braces for exact counts like \d{4} or ranges like \d{2,4}. By default they are greedy — they take as much as possible and give characters back only when the rest of the pattern fails. Apply <.+> to <b>bold</b> and it swallows everything, tags included.
Add a second question mark and the quantifier becomes lazy: <.+?> takes as little as possible, stopping at the first closing angle bracket. Some engines — PCRE, Java, .NET — also offer possessive quantifiers like a++, which consume input and never give it back; that prevents catastrophic backtracking on patterns like (a+)+b. JavaScript has neither possessive quantifiers nor atomic groups, so the practical defence is lazy matching and avoiding nested unbounded quantifiers.
Groups, alternation, and backreferences
Parentheses do two jobs. They group, so you can apply a quantifier to a sequence, and they capture, so you can refer to the matched text later. Use (?:...) when you only want grouping — a non-capturing group is cheaper to read and slightly faster. Alternation | has the lowest precedence of any operator, which is why cat|dog and (cat|dog) behave differently once you append something: cat|dogs matches "cat" or "dogs", while (cat|dog)s matches "cats" or "dogs".
Backreferences point at captured groups by number: \1 is whatever the first group matched. The pattern \b(\w+)\s+\1\b finds doubled words like "the the", because the second the must equal the first capture. Modern JavaScript also supports named groups, (?<year>\d{4}), which are far kinder to read in replacement strings — the replacement then references $<year>.
Ten patterns worth memorising
| Pattern | Matches | Typical use |
|---|---|---|
^\s+|\s+$ | Leading or trailing whitespace | Trim a string |
^\s*$ | Blank lines | Remove empty lines |
\b(\w+)\s+\1\b | A repeated word | Proofreading |
^[^\s@]+@[^\s@]+\.[^\s@]+$ | Email-shaped text | Form validation |
\b(?:\d{1,3}\.){3}\d{1,3}\b | IPv4-shaped text | Scanning logs |
\d{4}-\d{2}-\d{2} | An ISO date | Parsing and sorting |
<([a-z][a-z0-9]*)>.*?</\1> | A simple tag pair | Quick HTML scraping |
^\d+(?:\.\d{2})?$ | A money amount | Input validation |
#[0-9a-fA-F]{3,6}\b | A hex colour | Checking CSS |
(?<=\$)\d+(?:\.\d{2})? | Digits after a dollar sign | Extracting prices |
Memorising ten patterns beats knowing every token in the spec, because these ten cover the daily work. When a pattern misbehaves, change one thing at a time and watch matches update in the regex tester; when a bulk find-and-replace goes wrong, compare versions with the diff checker before committing.
Regex Tester — write a pattern, paste your text, and see matches and capture groups highlight live in your browser.
Related tools: Find and Replace Text · Diff Checker