Go REST

JavaScript regex tester

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

Subject

Highlighted result

Matches:0

Match details

Substitution

What is a regular expression?

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

The killer feature isn't matching, it's capturing: parentheses inside a pattern record what they matched, so you can extract structured pieces from unstructured text in one pass.

When to use this tool

  • Drafting a new pattern.Build the regex against representative subject text. The live highlight makes it instantly obvious when the pattern matches too much or too little.
  • Debugging an existing pattern.Drop the pattern in, paste the input that's failing, and see exactly which substring it's grabbing. Half the time the bug is greedy quantifiers eating across a boundary you didn't intend.
  • Designing a substitution.The Substitution panel shows you what String.replace would produce. Use $1, $2, etc. for numbered groups and $& for the full match.
  • Learning regex by experiment.Cheaper than reading the docs cover to cover. Make a small change to the pattern; watch the highlight shift.

The flags you'll actually use

  • g (global) - find every match, not just the first. Required for String.matchAll and for replace to hit more than one occurrence.
  • i (case-insensitive) - A matches a. Applies to the whole pattern.
  • m (multiline) - ^ and $ match line boundaries instead of just 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; required for \u{...} and \p{...} properties.
  • y (sticky) - matches only at lastIndex. Useful for tokenizing one match at a time without rescanning.

Worked examples

  • Extracting emails.(\w+)@(\w+\.\w+) with the g flag captures the local part and the domain. The match list above shows each as numbered groups.
  • Pulling out URLs.https?://\S+ grabs everything from http:// or https:// through 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 structured fields out of Common Log Format. The match list shows each by name.

Common pitfalls

  • Greedy by default..* matches everything it can. If you want "as little as possible", use .*? (lazy). Forgetting this is the most common regex bug.
  • Catastrophic backtracking.(a+)+ on input aaaaaaaaaaaaab can take seconds. Avoid nested quantifiers; flatten them with a single quantifier or a non-overlapping alternation.
  • Anchors and multi-line input.^ only matches the start of the whole string unless you set the m flag. $ is the same.
  • Escaping in JS string literals.A literal backslash in a regex needs two backslashes in a string source: "\\d+" in JS becomes the regex \d+. Use the literal /\d+/ form when possible.
  • Reusing a stateful regex object.A regex with the g or y flag carries lastIndex. Calling .test() twice with the same input on the same regex returns different results. Construct a fresh regex or reset lastIndex to 0.