Back to Utilities

Regex Tester

Test regular expressions live with match highlighting and group breakdowns.

Advertisement
Loading...

What this tool does

Regular expressions are a powerful pattern-matching notation used in text processing, validation, search-and-replace, and parsing. This tool lets you test regular expressions against sample text with live highlighting, capturing group breakdowns, and flag explanations. It supports JavaScript regex syntax including lookaheads, lookbehinds, named groups, and Unicode property escapes.

The tester immediately shows which parts of your text match the pattern, with different colors for full matches and capturing groups. It also flags common errors like unmatched parentheses, invalid quantifiers, and unsupported features. For complex expressions, you can enable verbose mode with comments and whitespace to make them more readable.

Usage Example

// Match an email address (simplified)
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

// Named capturing groups
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = re.exec("2024-03-15");
const { year, month, day } = match.groups;
// year: "2024", month: "03", day: "15"

// Lookahead: match "foo" only when followed by "bar"
/foo(?=bar)/.test("foobar")  // true

Common Edge Cases

  • Catastrophic backtracking can cause ReDoS (Regular Expression Denial of Service) on long inputs with patterns like (a+)+$. Use atomic groups or possessive quantifiers where available.
  • Greedy quantifiers (.*) match as much as possible by default. Use lazy quantifiers (.*?) for minimal matching.
  • The dot (.) does not match newlines by default. Use the s flag (dotAll) to make it match any character including newlines.
  • Anchors ^ and $ match start and end of string by default, not line. Use the m flag for multiline mode where they match line boundaries.
  • Character classes like [a-z] depend on the regex flavor and locale. JavaScript uses Unicode-aware classes by default, which may differ from older regex engines.

FAQ

What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (.*) match as much as possible while still allowing the overall pattern to match. Lazy quantifiers (.*?) match as little as possible. Use lazy when you want the shortest match, like extracting individual tags from HTML.
Why does my regex match the wrong text?
Common causes: forgetting to escape special characters (., *, +), using the wrong anchor (^ vs \A), or quantifier greediness. Test with simple inputs first, then add complexity.
Are JavaScript regexes the same as PCRE or POSIX?
No. JavaScript regexes are similar to Perl but lack some features (atomic groups, possessive quantifiers) and add some (Unicode property escapes, lookbehind). Always check your language's regex documentation.
Advertisement