Regex cheat sheet
| Pattern | Matches |
|---|---|
| . | Any character except a newline |
| \d \w \s | A digit, a word character (letter, digit, _), a whitespace character |
| \D \W \S | Anything that is not a digit, word character or whitespace |
| [abc] [^abc] [a-z] | One of a, b or c; anything except them; a range |
| ^ $ | Start and end of the text (or of each line with the m flag) |
| \b | A word boundary |
| * + ? | 0 or more, 1 or more, 0 or 1 of the previous item |
| {3} {2,5} {2,} | Exactly 3, between 2 and 5, at least 2 |
| *? +? | Lazy versions: match as little as possible |
| (abc) | Capture group, used as $1, $2 in replacements |
| (?:abc) | Group without capturing |
| (?<name>abc) | Named group |
| a|b | a or b |
| (?=x) (?!x) | Lookahead: followed by x, not followed by x |
| (?<=x) (?<!x) | Lookbehind: preceded by x, not preceded by x |
To match a special character literally, escape it with a backslash: \. matches a dot and \( a bracket.
Flags
- g (global): find every match, not just the first.
- i (ignore case):
catalso matches Cat and CAT. - m (multiline):
^and$match at the start and end of every line. - s (dotAll):
.also matches newlines. - u (unicode): handles emoji and other characters outside the basic range correctly, and enables
\p{L}style classes. - y (sticky): matches only at the exact current position.
Find and replace with groups
In the replacement, $1, $2 insert capture groups, $& inserts the whole match, and $<name> inserts a named group. For example, to turn dates from 2026-10-22 into 22/10/2026:
Common mistakes
- Greedy matching:
<.*>on<b>hi</b>matches the whole string. Use the lazy<.*?>to stop at the first>. - Forgetting anchors: to check a whole value (like a postcode), wrap the pattern in
^…$, or it will match part of a longer string. - Unescaped dots:
example.comalso matches "exampleXcom". Writeexample\.com. - Catastrophic backtracking: nested quantifiers like
(a+)+can freeze on long text. Keep patterns simple.
Other languages
Most syntax here works the same in Python, PHP (PCRE), Java and .NET, but details differ: lookbehind support, named group syntax ((?P<name>…) in Python) and flags are written differently. In code, remember to double backslashes inside normal string literals. Your text is tested in your browser and never sent anywhere.
Frequently asked questions
How do I test a regex online?
Type the pattern in the first box and your text below. Matches are highlighted instantly, with capture groups listed in a table.
Which regex flavour does this use?
JavaScript (ECMAScript), the same engine as Chrome and Node.js. Most patterns also work in Python, PHP and Java.
Why does my regex only find one match?
Add the g (global) flag to find every match instead of only the first.
How do I use capture groups in a replacement?
Put parts of the pattern in brackets, then use $1, $2 and so on in the Replace box.
How do I match a dot or a bracket?
Escape it with a backslash: \. for a dot, \( for a bracket, \? for a question mark.
Last updated October 2026 · More developer tools