Regex Cheat Sheet: JavaScript Patterns with Examples
· 3 min read
Regular expressions are compact, which makes them easy to forget when you only write one now and then. This page lists the JavaScript syntax you need, with a short example for each part.
Everything here is standard JavaScript (ECMAScript) behaviour. Try any pattern in the Regex Tester as you read.
Characters and classes
Each of these matches a single character.
| Token | Matches |
|---|---|
. | Any character except a line break |
\d | A digit, 0 to 9 |
\w | A letter, digit or underscore |
\s | A whitespace character |
\D | Any character that is not a digit |
\W | Any character that is not a word character |
\S | Any character that is not whitespace |
[abc] | One of a, b or c |
[^abc] | Any character except a, b or c |
[a-z] | One lowercase letter from a to z |
\d\d matches 42 in room 42. Note that \w covers only A-Z, a-z, 0-9 and _, so it does not match accented letters.
Anchors and boundaries
Anchors match a position, not a character.
| Token | Matches |
|---|---|
^ | The start of the string (or of a line, with m) |
$ | The end of the string (or of a line, with m) |
\b | A word boundary |
\B | A position that is not a word boundary |
\bcat\b matches cat in the cat sat but not in concatenate. \Bcat does the reverse and matches the cat inside concatenate.
Quantifiers
A quantifier says how many times the previous token repeats.
| Token | Meaning |
|---|---|
* | Zero or more times |
+ | One or more times |
? | Zero or one time |
{n} | Exactly n times |
{n,} | n or more times |
{n,m} | Between n and m times |
Quantifiers are greedy: they take as much text as they can. Add ? to make one lazy, as in *? and +?, so it takes as little as it can. On <b>bold</b>, <.+> matches the whole string, while <.+?> matches <b> and then </b>.
Groups
| Token | Meaning |
|---|---|
( ) | Capturing group, numbered from 1 |
(?: ) | Non-capturing group |
(?<name> ) | Named capturing group |
\1 | The same text as group 1 matched |
\k<name> | The same text as the named group matched |
a|b | Alternation: match the left or the right side |
(\w)\1 matches a doubled letter, such as ll in hello. gr(?:a|e)y matches gray and grey without capturing anything.
Lookarounds
A lookaround checks the text around a position without including it in the match.
| Token | Meaning |
|---|---|
(?=abc) | Followed by abc |
(?!abc) | Not followed by abc |
(?<=abc) | Preceded by abc |
(?<!abc) | Not preceded by abc |
\d+(?= USD) matches 50 in 50 USD, leaving USD out. (?<=\$)\d+ matches 19 in $19, leaving out the dollar sign.
Flags
Flags go after the closing slash, as in /cat/gi, or as the second argument to new RegExp().
| Flag | Name | Effect |
|---|---|---|
g | global | Finds all matches, not just the first |
i | ignore case | Treats upper and lower case as the same |
m | multiline | ^ and $ match at the start and end of every line |
s | dotAll | . matches line breaks too |
u | unicode | Treats the pattern and text as Unicode code points |
y | sticky | Matches only at the position in lastIndex |
d | indices | Records the start and end of each match and group |
Escaping
Put a backslash before a special character to match it literally: \. for a dot, \( for a bracket, \$ for a dollar sign. These characters need escaping: . * + ? ^ $ { } ( ) | [ ] \, and in a JavaScript literal, /.
In a string passed to new RegExp(), write the backslash twice, as in new RegExp("\\."), because the string uses up the first one.
Practical patterns
These come from the tester's Common patterns list:
| Use | Pattern |
|---|---|
| Email address (practical) | ^[^\s@]+@[^\s@]+\.[^\s@]+$ |
| URL (http or https) | \bhttps?:\/\/[^\s/$.?#][^\s]* |
| Date (YYYY-MM-DD) with named groups | \b(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])\b |
| Hex colour | #(?:[0-9a-f]{3}){1,2}\b |
| Trailing whitespace | [ \t]+$ |
No practical regex validates every email address. Use a simple one to catch typos, and confirm the address by sending an email.
Common mistakes
- Greedy matching.
<.+>swallows more than you expect. Use<.+?>or<[^>]+>. - Unescaped dots.
1.5also matches1x5. Write1\.5. - Forgetting
g. Without it, you get only the first match. - Anchors on multi-line text. Without
m,^and$match only at the ends of the whole string. - Catastrophic backtracking. Nested quantifiers such as
(a+)+give the engine exponentially many ways to fail, and can freeze a page or a server. The tester runs patterns in a background worker and stops any that run for more than a second. - Mixing flavours. The tester uses your browser's JavaScript engine, so a pattern behaves the same in browser and Node.js code. Python writes named groups as
(?P<name>...), and PCRE has possessive quantifiers that JavaScript lacks.
To replace many different words across a text at once, use Bulk Find and Replace. It can use regular expressions too, with $1 and $<name> in the replacements.
Try it
Paste any pattern from this page into the Regex Tester. It highlights matches, lists groups and explains the pattern piece by piece, all in your browser.