Regex Tester
Test and debug regular expressions with real-time matching.
About This Tool
Write a regular expression and test it against your text with real-time match highlighting. See all matches, captured groups, and match indices as you type. Supports JavaScript regex flags including global (g), case-insensitive (i), multiline (m), dotAll (s), and Unicode (u). Includes a quick reference of common regex patterns for faster development. Essential for debugging complex patterns, validating input formats, and learning regex syntax. Everything runs in your browser with no data transmitted.
What you provide
Regular expression pattern and test string
What you get
Highlighted matches with group details
How to Use
- Enter your regular expression in the pattern field.
- Type or paste your test string in the text area below.
- Matches are highlighted in real time with group details shown below.
When Regular Expressions Are the Right Tool
Regular expressions earn their place in four recurring scenarios: log parsing, input validation, search-and-replace transforms, and data extraction from semi-structured text. Extracting ISO timestamps from nginx access logs, validating that an input matches an IPv4 pattern, reformatting date strings from MM/DD/YYYY to YYYY-MM-DD, or pulling all URLs from a block of HTML comments — these are tasks where a single well-crafted pattern replaces dozens of lines of imperative string manipulation.
The line between 'right tool' and 'wrong tool' runs through parsing. Regular expressions are finite automata — they can only recognise regular languages. HTML, XML, JSON, and most programming languages are context-free grammars, meaning they have nested, recursive structure that a regex engine fundamentally cannot track. Trying to parse HTML with regex is the classic failure case: it works on simple inputs, breaks on edge cases, and becomes unmaintainable. Use a proper parser (DOMParser for HTML, JSON.parse for JSON, tree-sitter for code) whenever structure is nested or recursive.
For everything else, keep patterns focused and well-anchored. A regex that validates input should be anchored with ^ and $ so it cannot match a substring of invalid input. Prefer non-capturing groups (?:...) when you do not need the captured value — it is faster and makes the intent clear. Add verbose comments when patterns grow complex; JavaScript does not have an 'x' flag for whitespace-insensitive mode, but wrapping a pattern in a string and using String.raw keeps it readable.
Commonly Used Regex Patterns
Treat these as starting points, not production-ready validators. Email validation in particular is a rabbit hole — for critical paths, send a verification email rather than trying to validate the format perfectly.
// Basic email validation (not RFC 5322 complete, but catches 99% of typos)
const email = /^[^s@]+@[^s@]+.[^s@]+$/;
// IPv4 address
const ipv4 = /^((25[0-5]|2[0-4]d|[01]?dd?).){3}(25[0-5]|2[0-4]d|[01]?dd?)$/;
// ISO 8601 date (YYYY-MM-DD)
const isoDate = /^d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]d|3[01])$/;
// HTTP/HTTPS URL
const url = /^https?://([w-]+.)+[w-]+(/[w-./?%&=]*)?$/i;
// CSS hex color (#rgb, #rrggbb, #rrggbbaa)
const hexColor = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
// Usage
email.test('user@example.com'); // true
isoDate.test('2024-02-29'); // true (leap year check not included)
hexColor.test('#ff6600'); // trueRegex Performance Traps
- Catastrophic backtracking: patterns like (a+)+ or (a|a)+ cause exponential time on non-matching input because the engine explores an exponential number of paths — test all patterns against inputs designed to fail.
- Nested quantifiers on overlapping character classes: /([a-z]+)*$/ is the canonical ReDoS (Regular Expression Denial of Service) pattern; an attacker can craft input that hangs a Node.js server.
- Greedy .* across long strings: .* can match the entire input before backtracking; use .*? (lazy) or anchor with specific character classes when you know the shape of what you want.
- Unnecessary capture groups: every (...) without ?: allocates a capture slot and adds overhead; use non-capturing (?:...) when you only need grouping for alternation or quantifiers.
- Recompiling patterns in hot loops: create RegExp objects outside loops with const pattern = /foo/g and reset lastIndex manually rather than reconstructing on each iteration.
Frequently Asked Questions
- What regex flavor does this use?
- This tool uses JavaScript's built-in RegExp engine, which supports ECMAScript regex syntax. This is the same engine used in all modern browsers, Node.js, and Deno.
- What flags are supported?
- Global (g) to find all matches, case-insensitive (i), multiline (m) where ^ and $ match line boundaries, dotAll (s) where . matches newlines, and Unicode (u) for full Unicode support.
- Can I see captured groups?
- Yes. Each match shows its captured groups (parenthesized subexpressions) with their index and value, making it easy to debug complex patterns with multiple capture groups.
- What happens if my regex is invalid?
- The tool detects syntax errors in real time and displays a clear error message explaining what went wrong. Your test string is preserved so you can fix the pattern without re-entering your data.
- Is my data sent anywhere?
- No. All pattern matching runs entirely in your browser using JavaScript's native RegExp engine. No data is transmitted to any server.
Learn More
How to Write Better Regular Expressions: A Practical Guide
Learn regex patterns, performance tips, and common mistakes. Master character classes, quantifiers, and lookaheads with practical examples.
8 min read