Dev

Regex Tester

Test regular expressions with real-time highlighting, match count and group capture. JavaScript-syntax compatible.

โšกInstant๐Ÿ”’In your browserโœ“No signup
Live

What a regular expression is

A regex is a mini-language for describing text patterns. With a few characters you can say things like "three digits, a hyphen, four digits" (a phone number \d{3}-\d{4}) or "an email" (\b\w+@\w+\.\w+\b). They've existed since 1968 and remain the most concise tool for validating, extracting and replacing text.

Most-used characters

  • . โ€” any character except newline.
  • \d โ€” a digit (0-9).
  • \w โ€” a word character (letter, digit, underscore).
  • \s โ€” a whitespace (includes tab and newline).
  • + โ€” one or more.
  • * โ€” zero or more.
  • ? โ€” zero or one.
  • {n,m} โ€” between n and m repetitions.
  • ^ โ€” start of line (with m flag) or start of string.
  • $ โ€” end of line or string.
  • [abc] โ€” one of a, b or c.
  • [^abc] โ€” anything but a, b or c.

Common patterns worth memorizing

  • Simple email: \b[\w.-]+@[\w.-]+\.\w+\b โ€” catches most real emails.
  • URL: https?://\S+ โ€” http or https plus any non-space.
  • IPv4: \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b.
  • Date YYYY-MM-DD: \d{4}-\d{2}-\d{2}.
  • Hex color: #[0-9a-fA-F]{3,8}.
  • Digits only: ^\d+$ with m flag to validate line-by-line.

Flags and what they change

  • g (global): without it, regex returns only the first match. You almost always want it.
  • i (insensitive): ignores case. /Hello/i matches HELLO, hello, HeLlO.
  • m (multiline): makes ^ and $ match line starts/ends, not just string.
  • s (dotall): makes . also match newlines.
  • u (unicode): proper handling of complex Unicode characters.
  • y (sticky): match starts from lastIndex.

Common gotchas

  • Greedy vs lazy. .* is greedy: takes as much as possible. .*? is lazy: takes as little as possible. To extract "the nearest content between quotes," use lazy.
  • Forgetting the g flag. Without g, replace only changes the first match. "aaa".replace(/a/, 'b') gives "baa", not "bbb".
  • Escapes in strings. In JavaScript, "\d" isn't the same as "\\d". In literals /\d/ works; in strings you must double-escape.
  • Catastrophic backtracking. Patterns like (a+)+b on long input can hang the browser. Use linear patterns for big text.

When NOT to use regex

For parsing HTML, JSON, CSV or any nested format, regex is a bad idea. Those formats need real parsers. The famous Stack Overflow answer "you cannot parse [X]HTML with regex" is a universal truth: regex only handles regular languages, and HTML is not regular. Use the right parser (DOMParser, JSON.parse, a CSV library).

Productivity tips

If you'll reuse the same regex in multiple places, name and comment it. Regexes are illegible at first glance; a comment saves debugging time. In JavaScript, the x flag for whitespace tolerance isn't standard, but you can build the regex with new RegExp(string) from named pieces. In real code, consider validation libraries like Yup or Zod that abstract common regexes with declarative APIs.

FAQ

Which regex dialect does it use?

JavaScript (ECMAScript). Very close to PCRE.

Which flags are supported?

g, i, m, s, u, y โ€” combinable via checkboxes.

Does my text get uploaded?

No. 100% local in the browser.

Was this generator useful?