DevBench
All articles
regexjavascriptreference

Regular Expressions Cheat Sheet for JavaScript Developers

May 1, 20267 min read

Most developers use regex for the same 15–20 patterns repeatedly. This reference covers the patterns that actually appear in production JavaScript code, with copy-ready snippets and notes on edge cases.

Syntax quick reference

PatternMeaning
.Any character except newline (use s flag to include newlines)
\d / \DDigit / non-digit
\w / \WWord char (A-Za-z0-9_) / non-word
\s / \SWhitespace / non-whitespace
^ / $Start / end of string (m flag: start/end of line)
* + ?0 or more / 1 or more / 0 or 1
{n} {n,} {n,m}Exactly n / at least n / between n and m
(abc)Capture group — referenced as $1, $2...
(?:abc)Non-capturing group — groups but doesn't capture
(?<name>abc)Named capture group — referenced as $<name>
a|bAlternation: a or b
[abc]Character class: a, b, or c
[^abc]Negated class: anything except a, b, c
\b / \BWord boundary / non-word boundary
(?=abc)Positive lookahead: followed by abc
(?!abc)Negative lookahead: not followed by abc

Common patterns

Email address (practical)

/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/

Note: the only truly correct email regex spans thousands of characters. For most apps, validate server-side by sending a confirmation email.

URL

/^https?:\/\/[^\s/$.?#].[^\s]*$/i

IPv4 address

/^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/

ISO 8601 date (YYYY-MM-DD)

/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/

UUID v4

/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i

Semantic version (semver)

/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/

Hex color code

/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/

Phone number (E.164 international format)

/^\+[1-9]\d{1,14}$/

Strong password (min 8 chars, upper + lower + digit + symbol)

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z\d]).{8,}$/

Slug (URL-safe)

/^[a-z0-9]+(?:-[a-z0-9]+)*$/

Flags cheatsheet

  • g — global: find all matches (not just first). Required for String.matchAll().
  • i — case-insensitive
  • m — multiline: ^ and $ match line boundaries, not string boundaries
  • s — dotAll: . matches \n too
  • u — unicode: enables \p{...} property escapes and correct handling of surrogate pairs
  • d — indices: adds .indices to match results with start/end positions

Test your patterns interactively

Paste any pattern from above into the regex tester to see live match highlighting, capture group values, and the equivalent Python/Go/PHP code.

Try it yourself

Use the free browser-based Regex Tester on DevBench — no signup, runs entirely in your browser.

Open Regex Tester