Regex Tester
Test regular expressions against text in real-time. See matches, capture groups, replacement preview.
What are regular expressions?
Regular expressions (regex or regexp) are special patterns used to match, find, and manipulate text. Invented in the 1950s and standardized through Perl in the 1980s, regex is now built into virtually every programming language and text editor. They’re indispensable for: validating input (email addresses, phone numbers, postcodes), extracting data from text (parsing logs, web scraping), search and replace operations, syntax highlighting, route matching in web frameworks, and form validation. A regex pattern looks cryptic at first – \b\w+@\w+\.\w+\b matches email addresses – but the syntax has internal logic that becomes powerful once learned. This tester lets you build and debug regex patterns visually with instant match highlighting, capture group display, and replacement preview.
How to use this tool
- Enter regex pattern — The pattern without leading/trailing slashes. Use standard JavaScript regex syntax.
- Set flags — global (g): find all matches not just first; case insensitive (i); multiline (m): ^ and $ match line starts/ends; dotall (s): . matches newline.
- Enter test text — Paste any text you want to test against. The tool finds and highlights matches instantly.
- Optional: replacement string — If you want to replace matches, enter the replacement string. Supports backreferences like $1 for first capture group, $2 for second.
- Read the results — Count of matches, highlighted text showing matches, list of all matches with their position, and replacement preview if you entered one.
Regex syntax cheat sheet
Characters:
.any character (except newline unless ‘s’ flag)\ddigit (0-9),\Dnon-digit\wword char (a-z, A-Z, 0-9, _),\Wnon-word\swhitespace,\Snon-whitespace[abc]any of a, b, c;[^abc]none of a, b, c[a-z]range;[A-Za-z0-9]combined ranges
Quantifiers:
*zero or more;+one or more;?zero or one (optional){3}exactly 3 times;{2,5}2 to 5 times;{3,}3 or more
Anchors:
^start of string/line;$end\bword boundary;\Bnon-word-boundary
Groups:
(abc)capture group;(?:abc)non-capture group(?<name>...)named capture (modern)
Examples
- Email:
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b - URL:
https?:\/\/[^\s]+ - Phone (India):
[6-9]\d{9} - Indian PIN code:
\d{6} - US ZIP code:
\d{5}(?:-\d{4})? - Date (YYYY-MM-DD):
\d{4}-\d{2}-\d{2} - Hashtag:
#\w+ - Strong password:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$ - HTML tag:
<([a-z]+)([^>]*)>.*?<\/\1>
Tips & best practices
- Test regex with real data before deploying – edge cases break naive patterns
- Use non-capture groups (?:…) when you don’t need backreferences – faster and cleaner
- Always escape special characters in user input that becomes part of regex – new RegExp(userInput) is dangerous
- Use named capture groups (?
…) for readable code – replace $1 with named references - Beware of catastrophic backtracking – nested quantifiers like (a+)+ can hang the browser on long input
- For simple checks (does it contain ‘foo’?), use String.includes() not regex – faster and clearer
- Comment complex regex with the x flag (extended) – allows whitespace and # comments in pattern
Limitations & notes
This tester uses JavaScript’s RegExp engine (ECMA-262). Other languages have slightly different regex flavors: Python’s re module supports lookbehinds differently, PCRE (PHP, Perl) has more features like recursion. Most common patterns work the same across all engines. Very long inputs or patterns with catastrophic backtracking can freeze your browser – the test is real-time. Stop and refine if performance lags.
Frequently Asked Questions
How do I match a literal special character?
Escape it with a backslash. To match a literal period: \. To match a literal $: \$. To match a backslash itself: \\. Inside character classes [], most special chars don’t need escaping (except – and ]).
What’s the difference between * and +?
* matches zero or more (the previous element is optional and can repeat). + matches one or more (must appear at least once, can repeat). Example: ab*c matches ‘ac’, ‘abc’, ‘abbc’, ‘abbbc’. ab+c matches ‘abc’, ‘abbc’, ‘abbbc’ but NOT ‘ac’.
Why doesn’t my regex match across lines?
By default, . doesn’t match newlines, and ^/$ match start/end of the entire string. Enable ‘multiline’ (m) flag for ^/$ to match line boundaries. Enable ‘dotall’ (s) flag for . to match newlines too. For best multiline matching, both flags.
How do capture groups work?
(expr) creates a capture group – the matched substring is saved and can be referenced as $1, $2, etc. in replacements or m[1], m[2] in match results. Useful for extracting parts: ([a-z]+)@([a-z]+) on ‘hello@world’ captures ‘hello’ as group 1 and ‘world’ as group 2.
What’s a greedy vs lazy match?
Greedy (default) matches as much as possible. Lazy (add ?) matches as little as possible. Example on ‘bolditalic‘: <.+> matches the entire thing (greedy), <.+?> matches just (lazy). For HTML parsing, lazy is usually correct.
Can I do lookbehind in JavaScript?
Yes, modern JS supports (?<=...) (positive lookbehind) and (?
Is regex case-insensitive by default?
No – regex is case-sensitive by default. /abc/ matches ‘abc’ but not ‘ABC’. Add the ‘i’ flag (in our tester, the ‘case insensitive’ toggle) to make it case-insensitive: /abc/i matches both.
