Skip to content
HandyKit

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.

TokenMatches
.Any character except a line break
\dA digit, 0 to 9
\wA letter, digit or underscore
\sA whitespace character
\DAny character that is not a digit
\WAny character that is not a word character
\SAny 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.

TokenMatches
^The start of the string (or of a line, with m)
$The end of the string (or of a line, with m)
\bA word boundary
\BA 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.

TokenMeaning
*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

TokenMeaning
( )Capturing group, numbered from 1
(?: )Non-capturing group
(?<name> )Named capturing group
\1The same text as group 1 matched
\k<name>The same text as the named group matched
a|bAlternation: 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.

TokenMeaning
(?=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().

FlagNameEffect
gglobalFinds all matches, not just the first
iignore caseTreats upper and lower case as the same
mmultiline^ and $ match at the start and end of every line
sdotAll. matches line breaks too
uunicodeTreats the pattern and text as Unicode code points
ystickyMatches only at the position in lastIndex
dindicesRecords 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:

UsePattern
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.5 also matches 1x5. Write 1\.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.

Tools in this guide

  • Regex Tester

    Write a regular expression and see every match highlighted as you type, with the pattern explained part by part, capture groups, replacement and code you can paste into JavaScript.

  • Bulk Find and Replace

    Paste your text and a list of find → replace pairs, and every pair is applied in one go. Free and private: nothing is uploaded.

  • · 3 min read

    How to Find and Replace Many Words at Once

    Replace a whole list of words in one go: write find => replace pairs or paste them from a spreadsheet, swap words safely, and check every change afterwards.