Regex Tester

Matches, groups and a replace preview — evaluated in your browser.

/ /
—

Common flags

Greedy versus lazy quantifiers

By default * and + are greedy: they consume as much as possible while still allowing the overall pattern to match. Matching <.+> against <a><b> captures the whole string, not the first tag, because the dot happily eats the inner angle brackets. Adding a question mark makes the quantifier lazy — <.+?> stops at the first closing bracket. This single difference explains a large share of "my regex matches too much" problems. When the content cannot contain the delimiter, a negated character class is safer and faster still: <[^>]+>.

When regex is the wrong tool

Regular expressions cannot parse nested structures. HTML, JSON, source code and anything with balanced recursive delimiters are formally beyond what a regular language can describe, and every attempt to handle them with regex breaks on some real input. Use a parser for those. Regex is also a poor choice for email validation beyond a basic sanity check — a pattern that fully implements the address specification is thousands of characters long and still cannot tell whether the mailbox exists. Validate loosely with regex, then confirm by sending mail.

Catastrophic backtracking

A pattern with nested quantifiers, such as (a+)+b against a long run of "a" characters, can force the engine to try an exponential number of possibilities before failing. The result is a process that hangs for minutes or crashes — a real denial-of-service vector when the pattern runs on user-supplied input. Keep alternations and quantifiers simple, avoid nesting one quantifier inside another, and prefer negated character classes over .* when you can. If a pattern is slow on short input, it is dangerous on long input.

Practical tips

Everything here runs with JavaScript's built-in engine in your own tab. Nothing is sent anywhere, so you can safely test patterns against real data. For cleaning up the lines a match produces, try the duplicate line remover.