Regex Cheat Sheet
A quick reference for JavaScript regular expression syntax - character classes, quantifiers, groups, anchors, assertions, flags, and common ready-to-use patterns.
Character Classes
Match specific sets or ranges of characters.
. Any character except newline (or any character with s flag) \d Digit - [0-9] \D Non-digit - [^0-9] \w Word character - [a-zA-Z0-9_] \W Non-word character \s Whitespace - space, tab, newline, etc. \S Non-whitespace [abc] Any one of: a, b, or c [^abc] Any character except a, b, or c [a-z] Any character in the range a to z [a-zA-Z0-9] Letters and digits Quantifiers
Control how many times a pattern element repeats.
* Zero or more (greedy) + One or more (greedy) ? Zero or one - makes preceding element optional {n} Exactly n times {n,} n or more times {n,m} Between n and m times (inclusive) *? Zero or more (lazy - as few as possible) +? One or more (lazy) ?? Zero or one (lazy) {n,m}? Between n and m times (lazy) Anchors
Assert position in the string without consuming characters.
^ Start of string; start of each line with m flag $ End of string; end of each line with m flag \b Word boundary - between \w and \W \B Non-word boundary Groups & Alternation
Capture, group, and reference sub-patterns.
(abc) Capturing group - matched text accessible by index (?:abc) Non-capturing group - group without capturing (?<name>abc) Named capturing group - accessible via match.groups.name | Alternation - match either the left or right side \1, \2 Back-reference to capture group 1 or 2 \k<name> Back-reference to named capture group Assertions
Zero-width matches that check what's around the current position.
(?=abc) Positive lookahead - followed by abc (?!abc) Negative lookahead - not followed by abc (?<=abc) Positive lookbehind - preceded by abc (?<!abc) Negative lookbehind - not preceded by abc Flags
Modify how the entire pattern behaves.
g Global i Case insensitive m Multiline s Dot all u Unicode y Sticky d Indices Common Patterns
Ready-to-use patterns for everyday tasks. Test them in the Regex Tester →
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:\/?#[\]@!$&'()*+,;=%]* \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b #(?:[0-9a-f]{6}|[0-9a-f]{3})\b \b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\b \b(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?\b [0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12} ^[a-z0-9]+(?:-[a-z0-9]+)*$ \bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\b <([a-z][a-z0-9]*)(?:\s[^>]*)?>.*?<\/\1> Quick Tips
- Escape special characters with \ when matching them literally: \. matches a dot, not any character.
- Use non-capturing groups (?:…) when you only need grouping for quantifiers or alternation.
- Anchor patterns with ^ and $ to match the full string, not just a substring.
- Prefer lazy quantifiers (+?, *?) when you want the shortest possible match.
- Use [\s\S] to match any character including newlines without the s flag.
- Test edge cases: empty strings, Unicode characters, very long inputs, and nested patterns.
Related Tools
-
Regex Tester
Test and debug regular expressions with live match highlighting.
Regex -
JSON Formatter
Format, validate, and pretty-print JSON with error detection.
Format & Convert -
URL Encoder
Percent-encode special characters for safe use in URLs.
Encoding -
JWT Decoder
Decode and inspect JSON Web Token claims without a secret.
Auth & IDs
Frequently asked questions
Which regex flavor does this cheat sheet cover?
This cheat sheet covers JavaScript's built-in RegExp syntax as of ES2018+, including named capture groups, lookbehinds, the s (dotAll) flag, and the u (unicode) flag. Most syntax also works in Python, Java, Go, and PCRE with minor differences.
What is the difference between a greedy and a lazy quantifier?
Greedy quantifiers (*, +, ?) match as many characters as possible. Lazy quantifiers (*?, +?, ??) match as few as possible. For example, given the string <a><b>, the pattern <.*> greedily matches the entire string, while <.*?> lazily matches only <a>.
When should I use a non-capturing group (?:...)?
Use (?:...) when you need to group part of a pattern for alternation or quantification but do not need to capture the matched text. Non-capturing groups are slightly faster and keep your capture group numbering clean. Use (...) when you need to back-reference or extract the matched text.
What is the difference between a lookahead and a lookbehind?
A lookahead ((?=...)) asserts that what follows the current position matches the pattern, without consuming characters. A lookbehind ((?<=...)) asserts that what precedes the current position matches. Both have negative variants - (?!...) and (?<!...). Because they are zero-width, they do not appear in the matched text.
How do I match a newline with a dot?
By default, . matches any character except \n. Add the s (dotAll) flag to make . match \n as well. Alternatively, use [\s\S] which matches any whitespace or non-whitespace character, effectively covering every character including newlines - without needing the s flag.