How to use the regex tester
- Enter your pattern without the surrounding slashes, for example
\d{3}-\d{4}. - Toggle the flags you need (
g,i,m,s,u,y). - Paste test text. Matches are highlighted live and each capture group is listed.
- Optionally enter a replacement string, using
$1or$<name>for groups, to preview the result.
The tester uses your browser's own JavaScript regex engine. Your patterns and test data stay on your device and are not uploaded or stored.
JavaScript regex syntax at a glance
Regular expression flavours differ between languages. This tool uses the JavaScript (ECMAScript) flavour, so results match what RegExp, String.prototype.match and replace do in browsers and Node.js. Most syntax carries over to Python, PCRE and Java, but details such as named-group syntax and lookbehind support vary.
Flags
| Flag | Name | Effect |
|---|---|---|
g | global | Find all matches instead of stopping after the first. |
i | ignoreCase | Case-insensitive matching. |
m | multiline | ^ and $ match at the start and end of every line. |
s | dotAll | . also matches line breaks. |
u | unicode | Treat the pattern as Unicode code points; enables \p{...} property escapes. |
y | sticky | Match only at the position given by lastIndex. |
Building blocks
- Character classes:
[abc], ranges[a-z], negation[^0-9], shorthands\d(digit),\w(word character),\s(whitespace) and their negations\D,\W,\S. Without theuflag,\wand\dcover ASCII only. - Quantifiers:
*(0 or more),+(1 or more),?(0 or 1),{n},{n,},{n,m}. Append?to make them lazy:.*?. - Groups:
(...)captures,(?:...)groups without capturing,(?<year>\d{4})creates a named group, and\1or\k<year>refers back to it. - Anchors:
^,$and the word boundary\b. - Lookarounds:
(?=...)positive lookahead,(?!...)negative lookahead,(?<=...)positive lookbehind,(?<!...)negative lookbehind. They test context without consuming characters.
// Price amounts preceded by a dollar sign, without the sign
/(?<=\$)\d+(?:\.\d{2})?/g "Total: $42.50" → "42.50"
// Reformat dates with named groups
"2024-03-15".replace(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/, "$<d>.$<m>.$<y>")
// "15.03.2024"
Common patterns
| Purpose | Pattern | Limitations |
|---|---|---|
| Email (simple) | ^[^\s@]+@[^\s@]+\.[^\s@]+$ | A sanity check only. Full address syntax is far more complex; confirm by sending an email. |
| IPv4 address | ^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$ | Rejects octets above 255 and leading zeros. Does not handle IPv6. |
| Date (YYYY-MM-DD) | ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$ | Accepts impossible dates such as 2023-02-31. Validate with a date library. |
| URL (http/https) | ^https?:\/\/[^\s/$.?#][^\s]*$ | Loose check. Use the URL constructor for real parsing. |
For IP addresses specifically, the IP address converter parses and converts both IPv4 and IPv6.
Pitfalls
Catastrophic backtracking
Nested or overlapping quantifiers can make a backtracking engine like JavaScript's try an exponential number of paths when a match fails. A few dozen characters can freeze a browser tab or, on a server, enable a denial-of-service attack (ReDoS):
/^(a+)+$/.test("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!") // can hang
/^a+$/.test("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!") // instant
Avoid patterns like (a+)+, (a|a)* or (.*)*, keep alternatives mutually exclusive, and be specific: [^"]* is safer than .* inside quotes.
Other gotchas
- A regex with the
gflag rememberslastIndex, so callingtest()repeatedly on the same object alternates betweentrueandfalse. - When building a pattern from a string, backslashes must be doubled:
new RegExp("\\d+"). - Forgetting
^and$means a validator accepts any input that merely contains a match.
Frequently asked questions
Why does my regex work here but not in Python or PHP?
Flavours differ. Python writes named groups as (?P<name>...) and only supports fixed-width lookbehind; PHP's PCRE wraps patterns in delimiters. Escape rules for string literals also differ between languages.
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (.*) match as much as possible and then back off; lazy ones (.*?) match as little as possible. On <b>x</b>, <.*> matches the whole string while <.*?> matches only <b>.
Does JavaScript support lookbehind?
Yes, since ES2018, and unlike many engines it allows variable-length lookbehind. All current major browsers support it.
How do I match a literal dot or bracket?
Escape it with a backslash: \., \[, \(. Inside a character class most characters are literal, so [.] also matches a dot.