Guide
Regular Expression Quick Reference
Building blocks
| Pattern | Matches |
|---|---|
. | Any character except a line break (any character with the s flag) |
\d \w \s | A digit, a word character (letter, digit, underscore), whitespace |
\D \W \S | The opposite of each of the above |
[abc] [^abc] [a-z] | One of a set, anything not in the set, a range |
^ $ | Start and end of the input (of each line with the m flag) |
\b | A word boundary |
* + ? {n,m} | 0 or more, 1 or more, optional, between n and m times |
*? +? | Lazy versions: match as little as possible |
(x) (?:x) (?<name>x) | Capturing, non-capturing and named groups |
a|b | Either a or b |
(?=x) (?!x) | Lookahead: followed by x, not followed by x |
Flags
g(global) finds every match instead of stopping at the first.iignores letter case.mmakes^and$match at the start and end of each line.slets.match line breaks too.uenables full Unicode support, including\p{L}property escapes and correct handling of emoji.
Avoiding catastrophic backtracking
Patterns with nested quantifiers such as (a+)+$ or (\w|\d)*x can take exponential time on inputs that almost match, freezing a browser tab or, on a server, enabling a denial-of-service attack known as ReDoS. This tester runs each pattern in a background worker and stops it after 1.5 seconds. To fix a slow pattern, remove ambiguity: make alternatives mutually exclusive and avoid quantifying a group that is itself quantified.
Frequently asked questions
Which regex flavor does this use?
JavaScript (ECMAScript) regular expressions, as implemented by your browser. Most syntax is shared with PCRE, Python and Java, but some features such as possessive quantifiers and recursion are not available.
How do I use a captured group in the replacement?
Use $1, $2 and so on for numbered groups, $<name> for named groups, and $& for the whole match. Use $$ for a literal dollar sign.
Why does my pattern need double backslashes in code?
Inside a JavaScript string, backslash is also an escape character, so new RegExp("\\d+") is needed for \d+. With a regex literal (/\d+/) or in this tester, a single backslash is enough.