Skip to content
gohacks.dev
Tooling

Regex Cheat Sheet

A searchable regular expression cheat sheet: character classes, anchors, quantifiers, groups, lookarounds, flags, and tested patterns for emails, IPs, UUIDs, dates, semver and slugs.

Free · No sign-up · Your inputs stay in your browser

Reference

Regular expression cheat sheet

PatternMatchesNotes
.Any character except a newlineWith the s flag (dotAll), newlines too.
\d \w \sDigit, word character, whitespace\D \W \S are the negations. With the u flag, \d is still only 0-9 in JavaScript.
[abc] [^abc] [a-z]One of, none of, a rangeInside a class most metacharacters are literal; escape ] \ ^ -.
^ $Start and end of the stringOf each line with the m flag.
\b \BWord boundary, not a boundary\bcat\b matches cat but not concatenate.
* + ?0 or more, 1 or more, 0 or 1Greedy: they take as much as possible.
{3} {2,} {2,5}Exactly, at least, between
*? +? ??Lazy versionsTake as little as possible: <.+?> stops at the first >.
(abc)Capturing group$1 or \1 in the replacement.
(?<name>abc)Named group$<name> in JavaScript replacements; (?P<name>) in Python and older PCRE.
(?:abc)Non-capturing groupGroups without capturing; faster and keeps numbering simple.
a|bAlternationLowest precedence: ^a|b$ means (^a)|(b$).
\1 \k<name>Backreference(['"]).*?\1 matches a quoted string with matching quotes.
(?=x) (?!x)Lookahead, negative lookaheadMatch a position without consuming: \d+(?=px).
(?<=x) (?<!x)Lookbehind, negative lookbehindSupported in all current browsers, PCRE and Python (fixed width in Python).
\p{L} \p{N}Unicode letter, numberNeeds the u flag in JavaScript; works in PCRE with /u.
g i m s u yJavaScript flagsglobal, case-insensitive, multiline, dotAll, unicode, sticky. v is the newer unicode-sets flag.
^[\w.+-]+@[\w-]+(\.[\w-]+)+$Email (practical)Good enough for a form; RFC 5322 allows more. Confirm by sending a link.
^https?:\/\/[^\s/$.?#].[^\s]*$URL (http/https)Prefer new URL() and catch the error in code.
^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$IPv4 addressRejects 256 and above in each octet.
^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$UUIDUse the i flag.
^#?([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$Hex colour3, 6 or 8 digits (with alpha).
^\d{4}-\d{2}-\d{2}$ISO date (shape only)Accepts 2026-02-31; validate with a date library.
^([01]\d|2[0-3]):[0-5]\d$24-hour time
^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[\w.-]+)?(\+[\w.-]+)?$Semantic versionSimplified from the official semver.org regex.
^[a-z0-9]+(?:-[a-z0-9]+)*$URL slugLowercase words joined by single hyphens.
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{12,}$Password with mixed case and a digitLength matters more than classes; NIST 800-63B advises against composition rules.
^\s+|\s+$Leading or trailing whitespaceReplace with '' and the g flag to trim.
(\d)(?=(\d{3})+(?!\d))Thousands separator positionsReplace with $1, to get 1,234,567.

JavaScript syntax, which PCRE (PHP's preg_*), Python's re and most editors share for everything above except where noted. Test any pattern against real input with the regex tester before relying on it, and anchor it with ^ and $ when validating, or it matches a substring.

One syntax, small differences

Everything here is JavaScript syntax, which PCRE (PHP's preg functions), Python's re module and most editors share for the common cases. The differences show at the edges: named groups are (?P<name>) in older Python and PCRE, Python's lookbehind must be fixed width, and only some flavours support possessive quantifiers. MDN's regex cheat sheet covers JavaScript in detail.

Validation patterns

The email and URL patterns are practical, not complete: confirm an email by sending a link and parse URLs with a URL parser. Always anchor validation patterns with ^ and $, or they accept any string that contains a match.

Share

Frequently asked questions

What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (*, +) match as much as possible and backtrack; lazy ones (*?, +?) match as little as possible and extend only when needed.
How do I match a literal dot?
Escape it with a backslash, or put it in a character class: [.] matches only a dot.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Keep planning

Similar tools