Skip to main content

Regex Tester & Explainer

Test JavaScript regular expressions with live match highlights, named capture groups, replace preview, plain-English explanations, and a 500 ms Web Worker timeout guard.

By Lasita Engineering TeamLast reviewed: Examples verified by hand & automated test suite

Avoiding Catastrophic Backtracking (ReDoS)

Regular expressions are great for extracting structured fields from logs and validating input formats, but nested quantifiers like (a+)+$ or overlapping wildcards like .*.* can force JavaScript's backtracking engine into exponential work on non-matching strings.

// Safer explicit pattern for ISO dates (YYYY-MM-DD) with named capture groups
/^(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])$/

Practical Rules for Production Regexes

  1. Use named capture groups ((?<name>...)) so match results and replacement templates ($<name>) remain readable months later.
  2. Prefer negated character classes (like [^"]+) and bounded quantifiers (like \d{1,4}) over greedy wildcards.
  3. Never combine u and v flags — choose v when you need Unicode set intersection/subtraction, or u for standard Unicode code-point matching.
  4. Click Test 500 ms ReDoS guard in the tool above to watch (a+)+$ against 28 a characters + ! terminate safely without lagging the page.

Limits

This tester uses your browser's native JavaScript (ECMAScript) RegExp engine via matchAll() (handling Unicode emoji safely). Patterns are capped at 5,000 characters, match display is capped at 500 matches with a total count banner, and every run is bounded to 500 ms after worker startup.

Frequently Asked Questions

How does the 500 ms time limit protect against catastrophic backtracking (ReDoS)?

Every regex evaluation runs inside a dedicated background Web Worker rather than the main UI thread. The 500 ms timer starts only after the worker confirms it is ready (so slow script downloads never trigger false timeouts). If a pattern like (a+)+$ runs past 500 ms, the worker is terminated cleanly.

Why cannot the u and v flags be enabled at the same time?

In JavaScript (ECMAScript), the v flag (Unicode Sets) is an upgrade to the u flag (Unicode mode), and the language specification forbids combining both flags in the same RegExp constructor.

Are static ReDoS risk hints guaranteed to catch every slow regex?

No — static warnings are linear-time hints for common pitfalls like nested quantifiers or overlapping wildcards, not a mathematical proof. The real safety net is the strict 500 ms Web Worker execution timeout.

Read the full engineering reference: Practical Reference for Everyday Developer Data: JSON, Regex, Unix Timestamps, Base64 & WCAG Contrast →