Regex Cheatsheet
JavaScript RegExp quick reference: characters, classes, anchors, quantifiers, groups, assertions, flags, and String.replace tokens.
All processing on this page runs locally in your browser unless noted otherwise.
This sheet describes the JavaScript RegExp engine used by this site (not PCRE / .NET).
Characters
| Syntax | Meaning | Example |
|---|---|---|
. | Any character except line terminators (unless flag s). | a.c → abc, aac |
\d | A digit 0–9. | \d+ → 42 |
\D | A non-digit. | \D → a |
\w | Word character: letter, digit, or underscore. | \w+ → foo_1 |
\W | Non-word character. | \W → @ |
\s | Whitespace. | \s → space/tab |
\S | Non-whitespace. | \S → x |
\t | Tab character. | — |
\n | Newline. | — |
\r | Carriage return. | — |
\0 | NUL character. | — |
\xhh | Character by two hex digits. | \x41 → A |
\uhhhh | Unicode code unit (BMP). | \u4e2d → 中 |
\cX | Control character. | \cM → CR |
\ | Escape the next special character. | \. → literal . |
Character classes
| Syntax | Meaning | Example |
|---|---|---|
[abc] | One character from the set. | [aeiou] |
[^abc] | One character not in the set. | [^0-9] |
[a-z] | Inclusive character range. | [A-Za-z] |
[\d\s] | Escapes are allowed inside classes. | digit or space |
[[\]] | Literal brackets inside a class. | match [ or ] |
Anchors
| Syntax | Meaning | Example |
|---|---|---|
^ | Start of string (or line with flag m). | ^Hi |
$ | End of string (or line with flag m). | end$ |
\b | Word boundary. | \bcat\b |
\B | Not a word boundary. | \Bcat |
^…$ + m | With m, ^ and $ match line starts/ends. | with flag m |
Quantifiers
| Syntax | Meaning | Example |
|---|---|---|
* | 0 or more (greedy). | a* → "", a, aa |
+ | 1 or more (greedy). | a+ → a, aa |
? | 0 or 1 (greedy). | colou?r |
{n} | Exactly n times. | \d{4} |
{n,} | n or more times. | \d{2,} |
{n,m} | From n to m times. | \d{2,4} |
*? | 0 or more, lazy. | <.*?> |
+? | 1 or more, lazy. | — |
?? | 0 or 1, lazy. | — |
{n,m}? | n to m, lazy. | — |
Groups & alternation
| Syntax | Meaning | Example |
|---|---|---|
(…) | Capturing group. | (\d+)-(\d+) |
(?:…) | Non-capturing group. | (?:https?) |
(?<name>…) | Named capturing group. | (?<year>\d{4}) |
\1 / \k<name> | Backreference to a group. | (\w+)\s+\1 |
| | Alternation (OR). | cat|dog |
Lookaround
| Syntax | Meaning | Example |
|---|---|---|
(?=…) | Positive lookahead. | \d+(?=px) |
(?!…) | Negative lookahead. | foo(?!bar) |
(?<=…) | Positive lookbehind. | (?<=\$)\d+ |
(?<!…) | Negative lookbehind. | (?<!@)\b\w+ |
Flags
| Syntax | Meaning | Example |
|---|---|---|
g | Global — find all matches. | — |
i | Ignore case. | — |
m | Multiline — ^/$ match lines. | — |
s | dotAll — . matches newlines. | — |
u | Unicode mode. | — |
y | Sticky — match from lastIndex. | — |
d | Indices — expose match indices (when supported). | — |
Replacement
| Syntax | Meaning | Example |
|---|---|---|
$$ | Insert a literal $. | — |
$& | Insert the whole match. | — |
$` | Insert text before the match. | — |
$' | Insert text after the match. | — |
$1 … $99 | Insert numbered capture. | — |
$<name> | Insert named capture. | — |
How to use
- Browse by section or search for a token.
- Copy a token, then paste it into the tester pattern field.
- Open Templates for ready-made patterns you can try immediately.
FAQ
Does every token work in all browsers?
Most do. Named groups, lookbehind, and the d flag need relatively modern engines.
Why is this different from PHP or Python?
Dialects differ. Snippets on the tester page are illustrative; matching here is always JS RegExp.