Regex cheat sheet
A practical regex cheat sheet: character classes, anchors, quantifiers, groups, lookarounds and flags, with tested examples you can try in your browser.
How to use this cheat sheet
A regular expression (regex) is a pattern that describes text. You use it to search, validate, extract and replace text in code editors, command-line tools, databases and programming languages. The core syntax below works almost everywhere, but details differ between engines. This guide follows JavaScript, the flavor used by browsers, Node.js and our regex tester. Patterns are shown in plain text; type them exactly as written.
Characters and character classes
- . matches any single character except a line break (unless the s flag is set).
- \d matches a digit 0 to 9. \D matches anything that is not a digit.
- \w matches a word character: A to Z, a to z, 0 to 9 and underscore. \W is the opposite.
- \s matches whitespace: space, tab, line breaks and similar. \S is the opposite.
- [abc] matches one of a, b or c. [a-z] matches any lowercase ASCII letter.
- [^abc] matches any character except a, b or c.
- \p{L} matches any letter in any alphabet when the u flag is set. Use it instead of \w for non-English text.
To match a character that has a special meaning, escape it with a backslash. The special characters are . * + ? ^ $ { } ( ) [ ] | \ and /. For example, \. matches a literal period and \$ matches a dollar sign.
Anchors and boundaries
- ^ matches the start of the text, or the start of each line with the m flag.
- $ matches the end of the text, or the end of each line with the m flag.
- \b matches a word boundary, the position between a word character and a non-word character. \bcat\b matches cat but not the cat in concatenate.
- \B matches a position that is not a word boundary.
Quantifiers
- * means zero or more of the previous item.
- + means one or more.
- ? means zero or one, making the item optional. colou?r matches color and colour.
- {3} means exactly three. {2,} means two or more. {2,4} means two to four.
- Adding ? after a quantifier makes it lazy, matching as little as possible. <.+?> matches a single HTML tag, while <.+> would run to the last > on the line.
Groups, alternation and backreferences
- (abc) is a capturing group. It groups items and remembers the match.
- (?:abc) groups without capturing, which keeps group numbers tidy.
- (?<year>\d{4}) is a named capturing group.
- a|b matches a or b. Use a group to limit its scope: gr(a|e)y.
- \1 refers back to whatever group 1 matched. \b(\w+)\s+\1\b finds doubled words such as the the.
Lookarounds
Lookarounds check what comes before or after a position without including it in the match:
- (?=...) positive lookahead: \d+(?=px) matches 20 in 20px.
- (?!...) negative lookahead: \d+(?!px) matches numbers not followed by px, though it can still match part of one, such as the 2 in 20px. \d+(?!\d|px) prevents that.
- (?<=...) positive lookbehind: (?<=\$)\d+ matches 25 in $25.
- (?<!...) negative lookbehind: (?<!\$)\b\d+ matches numbers not preceded by a dollar sign.
Flags
- g (global): find every match, not just the first.
- i (ignore case): Cat, cat and CAT all match cat.
- m (multiline): ^ and $ match at line starts and ends.
- s (dotAll): . also matches line breaks.
- u (unicode): enables \p{...} classes and treats emoji and other astral characters as single characters.
- y (sticky): matches only at the current position.
- d (indices): records the start and end of each group.
- v (unicode sets): a newer, stricter version of u with set operations in classes, available in recent browsers.
Everyday patterns
These patterns are useful starting points. Real-world validation often needs more care, so test them against your own data.
- ISO date: \b\d{4}-\d{2}-\d{2}\b matches 2026-10-28. It does not check that the month or day exists.
- Hex color: ^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ matches #fff and #1a2b3c.
- Simple email check: ^[^\s@]+@[^\s@]+\.[^\s@]+$ catches obvious typos. Full email validation by regex is impractical; send a confirmation message instead.
- Trailing whitespace: [ \t]+$ with the m flag finds spaces at the end of lines.
- Blank lines: ^\s*$ with the m flag.
- Digits only: ^\d+$.
- A number with optional decimals: ^-?\d+(?:\.\d+)?$ matches 42, -7 and 3.14.
Test a regex in your browser
- Open the regex tester.
- Type your pattern without surrounding slashes, for example \b\w+@\w+\.com\b.
- Enter flags such as i or m. The tester always searches globally, so every match is listed.
- Paste your test text. Matches are highlighted as you type.
- Check the table below the preview for each match, its position (index) and the value of every capturing group.
The tester uses your browser's JavaScript engine, so results match what the same pattern does in JavaScript code. It lists up to 1,000 matches. If a pattern takes too long, for example because of catastrophic backtracking from nested quantifiers like (a+)+$, the test is stopped after a short time instead of freezing the page.
Regex testerTest regular expressions with live highlighting and capture groups.Search and replace with regex
To transform text with a pattern, use Find and replace in Regex mode. Capturing groups can be reused in the replacement with $1, $2 and so on. For example, find (\d{4})-(\d{2})-(\d{2}) and replace with $3/$2/$1 to turn 2026-10-28 into 28/10/2026. In Regex mode, matching is case-sensitive, so use a character class such as [Cc]at when you need to match both cases.
Find and replaceReplace every match of a word or regex pattern in your text.Tips for writing maintainable patterns
- Anchor validation patterns with ^ and $ so partial matches do not pass.
- Prefer specific classes like [^"]* over .* to avoid overmatching and backtracking.
- Use non-capturing groups when you do not need the value.
- Keep a set of test strings that should match and that should not, and rerun them after every change.