Skip to content

JavaScript regex tester

Build, test and debug JavaScript regular expressions. Matches are highlighted as you type, the match list shows every captured group, and the replacement preview shows what String.replace would give. All in your browser.

Flags

Highlighted result

Matches: 0
No subject text.

Match details

Substitution

What is a regular expression?

A regular expression (regex, regexp) is a small language for describing patterns of text. You can use it to find substrings ("any line that starts with HTTP/"), check input ("does this look like an email"), or rewrite text ("replace every IP with REDACTED"). The same pattern works in most programming languages, but the dialects differ in small ways. This page uses the JavaScript dialect.

The most useful part is not matching, it is capturing: parentheses inside a pattern record what they matched, so you can pull structured pieces out of plain text in one pass.

When to use this tool

  • Drafting a new pattern. Build the regex against sample subject text. The live highlight shows at once when the pattern matches too much or too little.
  • Debugging an existing pattern. Put the pattern in, paste the input that fails, and see exactly which substring it picks. Often the bug is a greedy quantifier that runs past a boundary you did not mean to cross.
  • Designing a substitution. The Substitution panel shows what String.replace would give. Use $1, $2 and so on for numbered groups and $& for the full match.
  • Learning regex by trying. Make a small change to the pattern and watch the highlight move. It is quicker than reading the whole reference.

The flags you will use

  • g (global): find every match, not just the first. Needed for String.matchAll and for replace to change more than one place.
  • i (case-insensitive): A matches a. Applies to the whole pattern.
  • m (multiline): ^ and $ match at line boundaries instead of only the start and end of the string.
  • s (dotall): . matches newlines. Without it, . never crosses a line break.
  • u (unicode): treats the pattern as a sequence of Unicode code points. Needed for \u{...} and \p{...} properties.
  • y (sticky): matches only at lastIndex. Useful for reading tokens one match at a time without scanning again.

The page shows at most 1000 matches and lists the first 50 in detail.

Worked examples

  • Extracting emails. (\w+)@(\w+\.\w+) with the g flag captures the local part and the domain. The match list shows each as numbered groups.
  • Pulling out URLs. https?://\S+ takes everything from http:// or https:// up to the next whitespace.
  • Validating UUIDs. ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ with i.
  • Named capture for log lines. (?<ip>\d+\.\d+\.\d+\.\d+) - - \[(?<ts>[^\]]+)\] "(?<method>\w+) (?<path>\S+) pulls fields out of Common Log Format. The match list shows each one by name.

Common mistakes

  • Greedy by default. .* matches as much as it can. If you want "as little as possible", use .*? (lazy). Forgetting this is the most common regex bug.
  • Catastrophic backtracking. ^(a+)+$ on a long run of a followed by b (28 a characters and one b) takes seconds, and the page cannot respond while it runs. Avoid nested quantifiers; use a single quantifier or an alternation whose branches do not overlap.
  • Anchors and multi-line input. ^ only matches the start of the whole string unless you set the m flag. $ works the same way.
  • Escaping in JS string literals. A backslash in a regex needs two backslashes in a string: "\\d+" in JS becomes the regex \d+. Use the literal form /\d+/ when you can.
  • Reusing a regex object that keeps state. A regex with the g or y flag keeps lastIndex. Calling .test() twice with the same input on the same regex gives different results. Make a new regex or set lastIndex back to 0.

FAQ

Which regex flavor does this tool use?

JavaScript regex (ECMAScript). It is close to PCRE. Older engines lack lookbehind (modern browsers support it), and some named-class shortcuts are missing.

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers (*, +, {n,}) match as much as possible. Lazy quantifiers (*?, +?, {n,}?) match as little as possible. The difference shows most on multi-line patterns: a greedy . runs across lines if you use the s flag.

How do I make a regex case-insensitive?

Turn on the i flag. It applies to the whole pattern. Recent browsers also accept an inline group like (?i:...) that applies to one part only; older browsers reject it.

Why is my regex slow on certain inputs?

Almost always catastrophic backtracking: nested quantifiers like (a+)+ or repeated alternations that overlap. Rewrite the pattern to be simpler, so each character can match in only one way.

Can I use named capture groups?

Yes. (?<name>...) defines a named group, and the match list shows it by name. Use $<name> in the replacement string.