Regular expression cheat sheet
A quick reference to the regex syntax shared by JavaScript, Python, PCRE and most editors, with a tiny example beside each token. The last table shows where the common flavours disagree, which is where most copy-pasted patterns break.
Single characters
| Token | Matches | Example |
|---|---|---|
. | Any character except a line break | h.t finds hat, hot, h9t |
\d / \D | A digit / not a digit | \d\d finds 42 in “room 42” |
\w / \W | Letter, digit or underscore / anything else | \w+ finds each word in “snake_case id” |
\s / \S | Whitespace / not whitespace | \s+ finds runs of spaces and tabs |
\t \n \r | Tab, line feed, carriage return | \r\n finds Windows line endings |
\. \* \? | The literal symbol | 3\.14 finds “3.14” but not “3714” |
The characters that need a backslash to be literal are . ^ $ * + ? ( ) [ ] { } | \ and, inside a JavaScript literal, /.
Character classes
| Token | Matches | Example |
|---|---|---|
[abc] | One of a, b or c | gr[ae]y finds grey and gray |
[^abc] | Any one character except those | [^,]+ finds each field between commas |
[a-z] | A range | [A-Fa-f0-9] finds one hex digit |
[-.] | Literal dash or dot | A dash first or last in the class is literal |
Anchors and boundaries
| Token | Position | Example |
|---|---|---|
^ | Start of text (or of each line with the m flag) | ^# finds lines that start with # |
$ | End of text (or of each line with m) | ;$ finds a trailing semicolon |
\b | Between a word character and a non-word character | \bcat\b finds “cat” but not “concat” |
\B | Not a word boundary | \Bcat finds the cat inside “concat” |
Quantifiers
| Token | Repeats the item before it | Example |
|---|---|---|
* | 0 or more | ab*c finds ac, abc, abbbc |
+ | 1 or more | \d+ finds 7 and 2048 |
? | 0 or 1 (optional) | colou?r finds color and colour |
{3} | Exactly 3 | \d{3} finds 3-digit runs |
{2,4} | 2 to 4 | x{2,4} finds xx, xxx, xxxx |
{2,} | 2 or more | \n{2,} finds blank-line gaps |
*? +? ?? | Lazy versions: as few as possible | ".+?" stops at the first closing quote |
Quantifiers are greedy by default: they take as much as they can, then give back only what the rest of the pattern needs.
Groups, alternation and references
| Token | Meaning | Example |
|---|---|---|
a|b | a or b | jpe?g|png finds jpg, jpeg, png |
(…) | Capturing group, numbered from 1 | (\w+)@ captures the name before @ |
(?:…) | Group without capturing | (?:ab)+ finds abab |
(?<name>…) | Named group (JavaScript, PCRE) | (?<user>\w+) |
\1 | Back-reference: the same text as group 1 | \b(\w+) \1\b finds “the the” |
$1 or \1 | Group 1 in a replacement string | Swap “Last, First”: (\w+), (\w+) → $2 $1 |
Lookarounds
Lookarounds check what is next to the current position without including it in the match.
| Token | Condition | Example |
|---|---|---|
(?=…) | Followed by | \d+(?=px) finds 12 in “12px” |
(?!…) | Not followed by | \d+(?!px|\d) finds 3 in “3em” |
(?<=…) | Preceded by | (?<=\$)\d+ finds 40 in “$40” |
(?<!…) | Not preceded by | (?<!-)\b\d+ skips negative numbers |
Flags
i– ignore case.g– find all matches, not just the first (JavaScript; other languages use a function such asfindall).m– multi-line:^and$work per line.s– “dot-all”:.also matches line breaks.u– Unicode mode in JavaScript, needed for\p{L}(any letter) and for emoji to count as one character.x– verbose mode in Python and PCRE: whitespace and#comments inside the pattern are ignored.
Where flavours differ
| Feature | JavaScript | Python re | PCRE (PHP, grep -P) | POSIX ERE (grep -E) |
|---|---|---|---|---|
| Named group | (?<n>…) | (?P<n>…) | Both forms | None |
Lazy *? | Yes | Yes | Yes | No |
| Lookbehind | Yes | Fixed width only | Yes, with limits on length | No |
\d | Yes | Yes (Unicode digits by default) | Yes | Use [0-9] or [[:digit:]] |
\p{L} | With u flag | Not in re | Yes | No |
Questions
Why does my pattern work in one tool but not another?
Check the flavour table first, then escaping. In a JavaScript or Python string literal, a backslash must often be doubled, so \d becomes "\\d" unless you use a regex literal or a raw string such as r"\d".
Should I use regex to parse HTML or JSON?
No. Use a real parser. Regex is fine for finding simple tokens inside text, but nested structures defeat it.
How do I try these patterns?
Paste any example into the regex tester, which also explains how to build a pattern against test cases.