Guide

A practical regex guide for JavaScript developers

The regular expression syntax you'll actually use in JavaScript — character classes, groups, lookarounds, flags and replacements — with examples you can try.

How we made this: Practical guide to the ECMAScript regular expression syntax as implemented in current browsers and Node.js. Examples were written for and checked in the ToolTarvern regex tester. Editorial policy.

Regular expressions are a small language for describing text patterns. This guide covers the parts of JavaScript’s regex syntax you’ll use most, with patterns you can paste into the regex tester. It runs your pattern in the browser’s own engine, highlights matches and shows capture groups.

The building blocks

PatternMatches
abcthe literal text “abc”
.any character except a line break (with the s flag, any character at all)
\d \w \sa digit, a “word” character [A-Za-z0-9_], whitespace
\D \W \Sthe opposite of each
[aeiou]one of the listed characters
[^0-9]any character except those listed
[a-z]a range
^ $start and end of the input (of each line with the m flag)
\ba word boundary
\. \( \\a literal ., ( or \, escaped

Quantifiers: how many

PatternMeaning
a*0 or more
a+1 or more
a?0 or 1 (optional)
a{3}exactly 3
a{2,4}2 to 4
a{2,}2 or more

Quantifiers are greedy: they match as much as possible. Add ? to make them lazy. On <b>one</b><b>two</b>, <b>.*</b> matches the whole string, while <b>.*?</b> matches each tag pair separately.

Groups and alternatives

  • (abc) is a capturing group. It remembers what matched, as $1, $2… in replacements.
  • (?:abc) groups without capturing. Use it with | or a quantifier when you don’t need the value.
  • (?<area>\d{3}) is a named group, available as match.groups.area and $<area> in replacements.
  • cat|dog matches either alternative.
  • \1 or \k<area> matches the same text a group already captured, e.g. (\w)\1 for a doubled letter.

Lookarounds: match only if…

PatternMeaning
x(?=y)x, only if followed by y
x(?!y)x, only if not followed by y
(?<=y)xx, only if preceded by y
(?<!y)xx, only if not preceded by y

Lookarounds check the surrounding text without including it in the match. For example, (?<=R)\d+ matches the number in “R250” but not the “R”.

Flags

FlagEffect
gfind all matches, not just the first
icase-insensitive
m^ and $ match at line breaks
s. also matches line breaks
uUnicode mode: correct handling of emoji and \p{…} property escapes like \p{L} (any letter)
van upgraded u mode with set operations in classes (newer engines)
y“sticky”: match only at lastIndex
dinclude start and end indices for groups

Worked examples

South African phone numbers in formats like “082 123 4567”, “082-123-4567” or “0821234567”:

(?<area>0\d{2})[\s-]?(\d{3})[\s-]?(\d{4})

Replace with +27 $<area> $2 $3 and then remove the leading 0 to normalise them. The regex tester opens with this example.

Find duplicated words (a common typo, “the the”):

\b(\w+)\s+\1\b

Use it with the gi flags.

Trim spaces at the start or end of each line: ^[ \t]+|[ \t]+$ with gm, replaced with nothing.

Pull amounts out of text: R\s?(\d{1,3}(?:[ ,]\d{3})*(?:\.\d{2})?) matches “R1 250.00”, “R 85” and “R12,500”.

Slug-safe text: replace [^\p{L}\p{N}]+ (with the gu flags) with -, then lowercase the result. The slug generator does this for you, and handles accents too.

In JavaScript code

const re = /(?<area>0\d{2})[\s-]?(\d{3})[\s-]?(\d{4})/g;
for (const m of text.matchAll(re)) console.log(m.groups.area, m.index);
const tidy = text.replace(re, '+27 $<area> $2 $3');

When you build a pattern from a string, double the backslashes, or use String.raw:

new RegExp(String.raw`\d{3}-\d{4}`, 'g');

Pitfalls

  • lastIndex with g or y: calling re.test() repeatedly on a global regex continues from the last match, so results alternate between true and false. Create the regex fresh, or reset lastIndex = 0.
  • Forgetting to escape: . matches any character. To match “1.5” exactly, write 1\.5.
  • Catastrophic backtracking: nested quantifiers like (a+)+$ or (\w+\s?)*$ can take exponential time on input that almost matches. That can freeze a browser tab, or a server (a ReDoS). Avoid nesting repeated groups that can match the same text. The regex tester runs patterns in a background worker and stops them after 1.5 seconds, so you’ll see the problem without your tab hanging.
  • Validating everything with one regex: a simple pattern plus code that’s easy to read beats a 300-character expression nobody can maintain.

Practise

Open the regex tester, paste some real text and build your pattern one piece at a time, checking the highlights as you go. To transform the matches afterwards, the text case converter and character counter are handy next steps.

Frequently asked questions

Why does my regex work in Python but not JavaScript?

Flavours differ. JavaScript has no possessive quantifiers or atomic groups, uses (?<name>…) for named groups (Python also accepts (?P<name>…)), and doesn’t support inline flags like (?i) in older engines. Test in the same engine you’ll run it in.

Should I validate emails with a regex?

Only loosely — check for something@something.something — then confirm the address by sending an email. A ‘complete’ email regex is huge and still rejects valid addresses.

What’s the difference between test() and match()?

regex.test(str) returns true or false. str.match(regex) returns the match details (or all matches with the g flag); str.matchAll(regex) gives every match with its groups.

Tools mentioned

GitHub Copilot

AI Coding

GitHub’s AI pair programmer. It suggests code as you type and offers a chat assistant inside popular code editors and on GitHub itself.

Main use
Writing boilerplate faster
Free plan
Not yet verified
Pricing
Not yet verified
Last reviewed
Not yet reviewed

Cursor

AI Coding

An AI-first code editor built on the same foundations as Visual Studio Code. It lets you chat with your codebase and ask the AI to make…

Main use
Refactoring
Free plan
Not yet verified
Pricing
Not yet verified
Last reviewed
Not yet reviewed

You may also need

Free tools and pages that help with the same job.

Explore categories