Validation/Regex Tester

Regex Tester

Match, capture groups and replacement preview

JavaScript regex syntax. Matches update as you type.
Pattern — input
Replacement (optional)
Matches0 matches
No matches.

A regular expression describes a pattern rather than a literal string, and the fastest way to get one right is to watch it match against real input as you edit. Writing a pattern blind and discovering its behaviour through failing tests is slower and teaches you less about why it went wrong.

This tester uses the JavaScript engine, so the syntax is what a browser or Node will execute — which is worth stating, because regex dialects genuinely differ. Lookbehind, named groups and unicode property escapes are supported in modern JavaScript but absent or spelled differently in POSIX tools, Python and Go’s RE2.

How to use it

  1. Enter a pattern and flagsThe g flag finds every match rather than the first, i ignores case, m changes how the anchors treat line breaks, and s lets a dot match a newline.
  2. Paste representative test textInclude the cases you expect to fail as well as the ones you expect to match. A pattern that matches everything you tried is not yet tested.
  3. Inspect the capture groupsEach match shows its groups, which is where most bugs actually live — a group that captured more or less than intended, or a greedy quantifier that swallowed the delimiter.
  4. Preview the replacementCheck a substitution before running it across a codebase. Group references in the replacement are the easiest thing to get subtly wrong.

Frequently asked questions

What is the difference between greedy and lazy quantifiers?

A greedy quantifier such as .* takes as much as it can and then gives back only what it must; a lazy one such as .*? takes as little as possible and expands only as needed. Matching an HTML tag with <.*> grabs everything from the first angle bracket to the last on the line, while <.*?> stops at the first closing bracket.

Why should I not parse HTML with a regular expression?

Because HTML is nested and regular expressions, in the formal sense, cannot count nesting. Any pattern that appears to work handles the examples you tried and fails on attributes containing angle brackets, comments, CDATA or unclosed tags. Use a parser; a regex is fine only for pulling a value out of markup you already control.

What is catastrophic backtracking?

When nested quantifiers create exponentially many ways to match, a pattern such as (a+)+$ against a long non-matching string can hang for minutes. If the input comes from a user, this is a denial-of-service vector known as ReDoS. Avoid nesting quantifiers, and prefer atomic groups or possessive quantifiers where the engine supports them.

How do I match a literal dot or bracket?

Escape it with a backslash, or put it in a character class where most metacharacters lose their meaning. The full metacharacter set is . * + ? ^ $ { } ( ) | [ ] \ — a pattern intended to match a filename or an IP address needs its dots escaped, otherwise a dot matches any character at all.

Can I use one regex for email validation?

Not usefully. The grammar in RFC 5322 permits quoted strings, comments and nested constructs that no practical regex handles, and the patterns people copy reject valid addresses. Check for an at sign with something either side, then send a confirmation email — delivery is the only real validation.

Related tools

Nothing left this tab. No request was made, no history was written. Validation · Regex Tester