regex --cheatsheet
Regex cheatsheet
The syntax of JavaScript regular expressions on one page. Every example was run when this page was built, and the highlighted part is what the pattern really matches. Click an example to open it in the tester.
| Token | Meaning | Example |
|---|---|---|
. | Any character except a line break (with the s flag: any character) | /h.t/ghat hot h t hit |
\d | A digit, 0 to 9 | /\d/gRoom 404 |
\D | Anything that is not a digit | /\D+/g404 not found |
\w | A word character: letter, digit or underscore | /\w+/gsnake_case, kebab-case |
\W | Anything that is not a word character | /\W+/ga, b; c |
\s | Whitespace: space, tab, line break | /\s+/ga b c |
\S | Anything that is not whitespace | /\S+/gtwo words |
[abc] | One of the listed characters | /gr[ae]y/ggray grey groy |
[^abc] | Any character that is not listed | /[^aeiou ]/gregex tool |
[a-z] | A character in the range | /[a-f0-9]+/gid: 00ff41 xyz |
\. | A literal dot. The backslash turns off the special meaning of . * + ? ( ) [ ] { } | ^ $ \ | /\./greez.cc |
\t \n \r | Tab, line feed, carriage return | /\t/ga b |
\x41 | The character with this hex code (two digits) | /\x41/gAbA |
\u00e9 | The character with this code point (four digits, or \u{1F600} with the u flag) | /\u00e9/gcafé |
\p{L} | A character with a Unicode property, here: any letter. Needs the u flag | /\p{L}+/gunaïve Köln 東京 42 |
Anchors match a position, not a character.
| Token | Meaning | Example |
|---|---|---|
^ | Start of the text (with the m flag: start of every line) | /^\w+/gfirst word only |
$ | End of the text (with the m flag: end of every line) | /\w+$/gonly the last |
\b | Word boundary: between a word character and anything else | /\bcat\b/gcat concat cats |
\B | Not a word boundary | /\Bcat/gcat concat cats |
A quantifier repeats the part right before it. Greedy takes as much as possible, lazy as little as possible.
| Token | Meaning | Example |
|---|---|---|
* | Zero or more times | /ab*/ga ab abbb |
+ | One or more times | /ab+/ga ab abbb |
? | Zero or one time (optional) | /colou?r/gcolor colour |
{3} | Exactly three times | /\d{3}/g12 345 6789 |
{2,} | Two or more times | /o{2,}/gso soo sooo |
{2,4} | Two to four times | /\d{2,4}/g1 22 4444 55555 |
*? +? ?? | Lazy: as few characters as possible | /<.+?>/g<b>bold</b> |
.* | Greedy by default: runs to the last possible match | /<.+>/g<b>bold</b> |
| Token | Meaning | Example |
|---|---|---|
(abc) | Capturing group: remembered as $1, $2, … in order of the opening bracket | /(\d+)px/gwidth: 120px |
(?:abc) | Non-capturing group: groups without remembering | /(?:ha)+/ghahaha ha |
(?<name>abc) | Named group: remembered as $<name> | /(?<user>\w+)@(?<host>\w+)/glukas@reez |
a|b | Alternation: either side | /cat|dog/gcat, dog, bird |
\1 | Backreference: the same text group 1 matched | /(\w)\1/ghello bookkeeper |
\k<name> | Backreference to a named group | /(?<q>['"]).*?\k<q>/gsay 'hi' or "bye" |
Lookarounds check what is next to the match without making it part of the match.
| Token | Meaning | Example |
|---|---|---|
(?=abc) | Lookahead: followed by abc | /\d+(?=px)/g12px 3em 40px |
(?!abc) | Negative lookahead: not followed by abc | /\d+(?!\d|px)/g12px 3 40 |
(?<=abc) | Lookbehind: preceded by abc | /(?<=\$)\d+/g$40 and 15 |
(?<!abc) | Negative lookbehind: not preceded by abc | /(?<![$\d])\d+/g$40 and 15 |
Used in the replacement text of String.replace and in the Replace tab of the tester.
| Token | Meaning | Example |
|---|---|---|
$1 | Text of group 1 | /(\w+) (\w+)/ghello world → world hello with $2 $1 |
$<name> | Text of a named group | /(?<d>\d\d)\.(?<m>\d\d)\./g14.11. → 11/14 with $<m>/$<d> |
$& | The whole match | /\d+/g3 apples, 12 pears → [3] apples, [12] pears with [$&] |
$` | Everything before the match | /b/abc → a(a)c with ($`) |
$' | Everything after the match | /b/abc → a(c)c with ($') |
$$ | A literal dollar sign | /USD /gUSD 40 → $40 with $$ |
Flags follow the closing slash, as in /pattern/gi, and change how the whole pattern works.
| Flag | Meaning |
|---|---|
g | Global: find all matches instead of stopping at the first |
i | Ignore case: a also matches A |
m | Multiline: ^ and $ match at the start and end of every line |
s | Dot all: . also matches line breaks |
u | Unicode: treats the pattern as code points, enables \p{…} and \u{…}, rejects unknown escapes |
y | Sticky: the match must start exactly at lastIndex |
d | Indices: also report start and end of every group. The tester always sets it to show positions |