Regex Tester

Test JavaScript regular expressions against sample text, inspect matches and debug patterns instantly.

Back to all tools on ToolForge

More in Developer Tools



Matches


About Regex Tester

This regex tester lets you validate JavaScript regular expressions against sample text and inspect every match, capture group, and pattern behavior in real-time. Whether you're debugging a complex pattern, learning regex syntax, or verifying email/URL validation rules, this tool provides immediate visual feedback.

How Regex Pattern Matching Works

The JavaScript regex engine processes your pattern through several steps:

  1. Compilation: The pattern string is compiled into an internal representation
  2. Matching: Engine compares pattern against input text character by character
  3. Backtracking: When a path fails, engine backtracks to try alternative matches
  4. Result: Returns matched substrings, capture groups, and position information

JavaScript Regex Flags

Flag Name Effect
g Global Find all matches (not just first)
i Case-insensitive Ignore case when matching
m Multiline ^ and $ match line boundaries
s dotAll . matches newline characters
u Unicode Enable full Unicode support
y Sticky Match from current index only

Common Regex Patterns

Pattern Description Example Match
\d+ One or more digits 123, 42, 0
[a-zA-Z]+ One or more letters hello, World, abc
\w+ Word characters (alphanumeric + _) user_name, test123
\s+ One or more whitespace " ", "\t", "\n"
^\w+@\w+\.\w+$ Simple email pattern [email protected]
https?://\S+ HTTP/HTTPS URLs https://example.com

Capture Groups Explained

Capture groups extract matched substrings for later use:

// Pattern with capture groups
const regex = /(\w+)@(\w+\.\w+)/g
const text = "Contact: [email protected]"
const match = text.match(regex)
// match[0] = "[email protected]" (full match)
// Group 1 = "user" (username)
// Group 2 = "example.com" (domain)

Greedy vs Lazy Quantifiers

Quantifiers control how many times a pattern element matches:

Greedy Lazy Behavior
* *? 0 or more (greedy vs lazy)
+ +? 1 or more (greedy vs lazy)
? ?? 0 or 1 (greedy vs lazy)
{n,m} {n,m}? n to m (greedy vs lazy)
// Greedy: matches as much as possible
/<.*>/.exec("<div>hello</div>")  // "<div>hello</div>"

// Lazy: matches as little as possible
/<.*?>/.exec("<div>hello</div>")  // "<div>"

How to Test Regular Expressions

  1. Enter your pattern: Type your regex pattern in the "Regex pattern" field (e.g., \d+).
  2. Enter test text: Paste or type the text you want to test against in the "Text" field.
  3. Click Test: The tool will execute your regex and display all matches.
  4. Review results: Examine matched substrings to verify your pattern works correctly.
  5. Refine and retest: Adjust your pattern as needed and click Test again.

Tips for Effective Regex Testing

Frequently Asked Questions

What is regex and how does pattern matching work?
Regex (regular expressions) is a sequence of characters that defines a search pattern. The regex engine processes your pattern and compares it against input text character by character, using backtracking to try different matching paths when needed. Metacharacters like ., *, +, ? have special meanings that control matching behavior.
What regex flags does JavaScript support?
JavaScript supports these regex flags: g (global - find all matches), i (case-insensitive), m (multiline - ^ and $ match line boundaries), s (dotAll - . matches newlines), u (Unicode), y (sticky - matches from current index). This tool uses the 'g' flag by default.
What are capture groups and how do I use them?
Capture groups (parentheses in regex) extract matched substrings for later use. Use (pattern) for numbered groups, (?:pattern) for non-capturing groups, and (?<name>pattern) for named groups. Access captured values via match arrays or the groups property.
Why is my regex matching too much or too little?
Common causes: greedy quantifiers (*, +) match as much as possible - use lazy versions (*?, +?) for minimal matching. Unescaped special characters (. matches any char) can cause unexpected matches. Character classes [^abc] negate - ensure this is intentional.
What is the difference between match() and matchAll()?
match() with /g flag returns array of matched strings only. matchAll() returns iterator with full match objects including capture groups, indices, and named groups. Use matchAll() when you need detailed match information beyond just the matched text.
How do I escape special characters in regex?
Escape these metacharacters with backslash: . * + ? ^ $ { } ( ) | [ ] \. To match a literal dot use \., literal asterisk use \*, etc. In JavaScript strings, use double backslash (\\) because \ itself needs escaping.