Regex cheat sheet
| Pattern | Matches |
|---|---|
. | Any character except a newline (add the s flag to include newlines) |
\d \w \s | A digit, a word character (letter, digit or underscore), whitespace |
\D \W \S | The opposite of each of the above |
[abc] [^abc] | Any one of a, b or c; any character except those |
[a-z] | Any character in the range a to z |
^ $ | Start and end of the text (or of each line with the m flag) |
\b | A word boundary |
* + ? | Zero or more, one or more, zero or one |
{3} {2,5} {2,} | Exactly 3, between 2 and 5, 2 or more |
*? +? | "Lazy" versions that match as little as possible |
(abc) | A capture group, reusable as $1 in replacements or \1 in the pattern |
(?:abc) | A group that doesn't capture |
(?<name>abc) | A named capture group |
a|b | a or b |
\. \* | A literal dot or star. Put a backslash before any special character to match it literally |
Flags
gglobal: find every match, not just the first.iignore case:catalso matches "Cat" and "CAT".mmultiline:^and$match at the start and end of each line.sdot all:.also matches newlines.uunicode: treat the pattern as Unicode, needed for some emoji and\p{…}classes.
Regex flavours differ
This tester uses JavaScript's regex engine, the one built into every browser and Node.js. Python, PHP, Java and other languages support almost all of the same core syntax, but differ in details. For example, Python writes named groups as (?P<name>…) and uses \1 rather than $1 in replacements. Check your language's documentation for advanced features.
Watch out for slow patterns. Nested repeats like
(a+)+ can take an extremely long time on text that almost matches, a problem called catastrophic backtracking. This tester stops any pattern that runs for more than a second.