Copy-ready JavaScript regex patterns
All content runs locally in your browser

| Expression | Description |
|---|---|
. or [^\n\r] | any character excluding a newline or carriage return |
[A-Za-z] | alphabet |
[a-z] | lowercase alphabet |
[A-Z] | uppercase alphabet |
\d or [0-9] | digit |
\D or [^0-9] | non-digit |
_ | underscore |
\w or [A-Za-z0-9_] | alphabet, digit or underscore |
\W or [^A-Za-z0-9_] | inverse of w |
\S | inverse of s |
| Expression | Description |
|---|---|
| space | |
\t | tab |
\n | newline |
\r | carriage return |
\s | space, tab, newline or carriage return |
| Expression | Description |
|---|---|
[xyz] | either x, y or z |
[^xyz] | neither x, y nor z |
[1-3] | either 1, 2 or 3 |
[^1-3] | neither 1, 2 nor 3 |
^ after the opening [ to “negate” the character set.. means a literal period.| Expression | Description |
|---|---|
\. | period |
\^ | caret |
\$ | dollar sign |
\| | pipe |
\\ | back slash |
\/ | forward slash |
\( | opening bracket |
\) | closing bracket |
\[ | opening square bracket |
\] | closing square bracket |
\{ | opening curly bracket |
\} | closing curly bracket |
| Expression | Description |
|---|---|
\\ | back slash |
\] | closing square bracket |
^ must be escaped only if it occurs immediately after the opening [ of the character set.- must be escaped only if it occurs between two alphabets or two digits.| Expression | Description |
|---|---|
{2} | exactly 2 |
{2,} | at least 2 |
{2,7} | at least 2 but no more than 7 |
* | 0 or more |
+ | 1 or more |
? | exactly 0 or 1 |
| Expression | Description |
|---|---|
^ | start of string |
$ | end of string |
\b | word boundary |
\w.\w and the second character is \W.\w.| Expression | Description |
|---|---|
foo|bar | match either foo or bar |
foo(?=bar) | match foo if it’s before bar |
foo(?!bar) | match foo if it’s not before bar |
(?<=bar)foo | match foo if it’s after bar |
(?<!bar)foo | match foo if it’s not after bar |
| Expression | Description |
|---|---|
(foo) | capturing group; match and capture foo |
(?:foo) | non-capturing group; match foo but without capturing foo |
(foo)bar\1 | \1 is a backreference to the 1st capturing group; match foobarfoo |
string.match(regexp)string.matchAll(regexp)string.replace(regexp, callback)\N is a backreference to the Nth capturing group. Capturing groups are numbered starting from 1.This Regex cheatsheet is a fast reference for JavaScript regular expressions (RegExp): boundaries, matching, groups, escaping rules, and the most common building blocks you’ll see in day-to-day coding.
If you’re debugging a tricky pattern, pair this page with our Regex tester to try real inputs and verify edge cases.
Who is this for?
When it saves time
When you already know what you want (for example “match a whole word” or “capture a group”), but you don’t want to remember the exact syntax.
The cheatsheet focuses on practical patterns and plain-English meaning. If you need help experimenting, open the tester and iterate: write a pattern, try inputs, and add edge cases.
Start with the smallest correct block
Pick one row from the tables (for example, boundaries like ) and validate it on sample text.
Add repetition with quantifiers
Use , , or range quantifiers like .
Capture only what you need
Use when you need a group (for extraction or backreferences), and use non-capturing when you don’t.
Test edge cases and performance
Try empty strings, very long strings, and “almost matches.” If input can come from users, avoid patterns that can blow up runtime.
How to interpret results
Extract numbers from a string
Input: "Order #A19: 3 items"
Pattern: /\\d+/g
Result: (two matches)
Apply it to extract IDs, counts, or timestamps.
Match a whole word (not a substring)
Input: "cat scatter catalog"
Pattern: /\\bcat\\b/g
Result: matches only the standalone
Useful for search and replace without accidental partial matches.
Validate a simple slug
Input: "my-post-2026"
Pattern: /^[a-z0-9-]+$/
Result: pass / fail
Keep it strict when the value is used in URLs.
Use lookaround to match context
Input: "total=42; tax=7"
Pattern: /(?<=total=)\\d+/
Result:
You match the number without consuming the prefix.
Want to try these instantly? Open Regex tester and paste the input + pattern.
Basic checks like only digits or a simple slug.
Extract IDs, status codes, and timestamps from messy text.
Use boundaries to avoid changing partial words.
Normalize whitespace and remove unwanted characters.
Quick pattern checks (but don’t rely on regex alone).
Segment text before counting or aggregating.
When regex may not be a good fit
Pro tip: Write the simplest pattern that works, then tighten it. Regex gets hard when you try to be clever too early.
Regex isn’t “calculation” in the math sense, but the rules are still precise. Here are the two ideas that explain most surprises: boundaries and quantifiers.
Quantifier ranges
A range quantifier like means “repeat between and times.”
Word boundaries
The boundary matches a transition between a “word character” and a “non-word character.” In JavaScript, that word set is roughly:
Lookaround is “match without consuming”
A lookahead/behind checks context but doesn’t become part of the match. It’s handy when you want to keep the output clean.
Greedy vs lazy quantifiers
Quantifiers are greedy by default (they match as much as possible). Adding makes them lazy.
Engine differences
Not all regex engines support the same features (for example, lookbehind). If a pattern fails in another language, verify engine support and syntax.
In JavaScript, . doesn’t match newline by default. Use the flag (dotAll) if you truly want dot to include newlines.
Capturing ( ) stores the matched substring for extraction/backreferences. Non-capturing (?: ) groups without creating a numbered capture.
It’s usually greediness. Try a lazy quantifier like or tighten the character set.
Different engines support different features and edge-case behavior (Unicode, lookbehind, possessive quantifiers, etc.). Always check your target runtime.
It can be, but avoid patterns that may backtrack catastrophically. If you must run complex patterns on user text, limit input length and test worst-case strings.
For hands-on testing, use Regex tester.
Use a simple chronometer (stopwatch) to track elapsed time down to milliseconds. Runs locally in your browser.
Normalize email addresses to a standard format for easier comparison. Useful for deduplication and data cleaning. Runs locally in your browser.
Estimate the time needed to consume a total amount at a constant rate, and get an expected end time. Runs locally in your browser.
Parse and decode your JSON Web Token (JWT) and display its content. All computation runs locally in your browser.
Know which file extensions are associated to a MIME type, and which MIME type is associated to a file extension. Includes a full MIME types table.
Generate random Lorem Ipsum placeholder text with customizable paragraphs, sentences, and word counts. Runs locally in your browser.