Regex Tester
Regex Tester
Regular expressions, commonly called regex, are a formal language for describing text patterns. They allow you to search, match, extract, and transform text with a single compact expression that would otherwise require dozens of lines of procedural code. Mastery of regular expressions is one of the most transferable skills in software development, applicable across virtually every programming language and text processing tool.
Regex is used in many contexts. Developers use it for form validation (email addresses, phone numbers, passwords), search-and-replace in code editors, log parsing, and data extraction from unstructured text. Data analysts apply regex to clean datasets, extract fields from CSV files, and validate data quality. System administrators use regex in command-line tools like grep, sed, and awk to filter log files, process configuration files, and automate text transformations. Even modern IDEs rely on regex for advanced find-and-replace operations.
Testing a regular expression before deploying it into production code is essential. A poorly written regex can silently fail to match valid input, produce false positives, or cause catastrophic backtracking that freezes your application. This tool provides two modes: Match mode, which finds all occurrences of a pattern and shows detailed match information including positions and capture groups, and Replace mode, which applies a regex substitution and shows the transformed result along with the replacement count.
Using the Regex Tester is straightforward. Select a mode, enter your regex pattern (without delimiters), specify flags, and provide a test string. The results update in real time.
Match Mode (Default)
Match mode finds all occurrences of your pattern in the test string. Enter the raw pattern in the Regex Pattern field, set flags (default g for global), and type or paste your test string. The tool auto-calculates and displays the total number of matches and a detailed list showing each match's text and position.
Example 1 — Extracting digits from a string:
Enter pattern \d+ with flags g and test string Order #12345 was placed on 2026-06-16. The tool finds three matches:
- Match 1:
"12345"at index 7 - Match 2:
"2026"at index 28 - Match 3:
"06"at index 33 - Match 4:
"16"at index 36
Example 2 — Finding email addresses:
Enter pattern [\w.-]+@[\w.-]+\.\w+ with flags gi and test string Contact support@example.com or sales@company.org for assistance. The tool finds two matches:
"support@example.com"at index 8"sales@company.org"at index 37
Example 3 — Using capture groups:
Enter pattern (\w+)@(\w+)\.(\w+) with flags g and test string user@example.com. The match shows:
- Match 1:
"user@example.com"at index 0, groups:[user, example, com]
Replace Mode
Replace mode substitutes matched text with a replacement string. Click the Replace button to execute. The result shows the transformed string and the count of replacements made.
Example 4 — Replacing digits with a placeholder:
Enter pattern \d+, flags g, replacement [REDACTED], and test string SSN: 123-45-6789, Phone: 555-1234. Click Replace. Result: SSN: [REDACTED]-[REDACTED]-[REDACTED], Phone: [REDACTED]-[REDACTED] with 6 replacements.
Example 5 — Using captured groups in replacement:
Enter pattern (\w+)@(\w+)\.(\w+), flags g, replacement $2 at $1 dot $3, and test string Email: user@example.com. Click Replace. Result: Email: example at user dot com with 1 replacement.
Common Flags
| Flag | Name | Effect | Example |
|---|---|---|---|
g | Global | Find all matches, not just the first | /\d+/g finds all numbers |
i | Case-insensitive | Ignore case when matching | /hello/i matches "Hello", "HELLO", "hello" |
m | Multiline | ^ and $ match start/end of each line | /^test/m matches lines starting with "test" |
s | Dotall | . matches newline characters | /.*/s matches across multiple lines |
u | Unicode | Enable Unicode property escapes | /\p{Emoji}/u matches emoji |
y | Sticky | Match only from lastIndex position | /foo/y matches only at lastIndex |
| Pattern | Meaning | Matches | Does Not Match |
|---|---|---|---|
\d | Digit character | 5, 0, 9 | a, _, space |
\w | Word character (letter, digit, underscore) | a, Z, 5, _ | -, @, space |
\s | Whitespace character | space, tab, newline | a, 5 |
. | Any character except newline (unless s flag) | a, 5, @ | newline |
* | Zero or more of preceding element | "a", "aa", "" in /a*/ | — |
+ | One or more of preceding element | "a", "aa" in /a+/ | "" |
? | Zero or one of preceding element (optional) | "a", "" in /a?/ | "aa" |
{n} | Exactly n occurrences | "aaa" in /a{3}/ | "aa", "aaaa" |
{n,} | n or more occurrences | "aaa", "aaaa" in /a{3,}/ | "aa" |
{n,m} | Between n and m occurrences | "aa", "aaa" in /a{2,3}/ | "a", "aaaa" |
[abc] | Character class — any one of a, b, c | "a", "b" | "d", "ab" |
[^abc] | Negated character class | "d", "e" | "a", "b", "c" |
(group) | Capturing group | Captures matched text for backreference | — |
(?:group) | Non-capturing group | Groups without capturing | — |
a|b | Alternation — a or b | "a", "b" | "c" |
^ | Start of string (or line with m) | Positional anchor | — |
$ | End of string (or line with m) | Positional anchor | — |
\b | Word boundary | Position between \w and \W | — |
\B | Non-word boundary | Position within \w\w or \W\W | — |
(?=...) | Lookahead | Followed by pattern, without consuming | — |
(?!...) | Negative lookahead | Not followed by pattern | — |
(?<=...) | Lookbehind | Preceded by pattern (ES2018+) | — |
(?<!...) | Negative lookbehind | Not preceded by pattern (ES2018+) | — |
Common Regex Patterns
| Use Case | Pattern | Example Matches | Notes |
|---|---|---|---|
| Email address | [\w.-]+@[\w.-]+\.\w+ | user@example.com, first.last@company.co.uk | Does not validate TLD length; use a proper parser for production |
| URL | https?://[\w./?#=-]+ | https://example.com/page?q=1 | Does not handle all URL components; prefer URL API in code |
| US Phone | \d{3}[-.]?\d{3}[-.]?\d{4} | 555-123-4567, 555.123.4567 | Accepts 5551234567; adjust for international formats |
| Date (ISO 8601) | \d{4}-\d{2}-\d{2} | 2026-06-16 | Does not validate month/day ranges; use Date parser |
| IPv4 Address | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} | 192.168.1.1, 10.0.0.255 | Does not validate octet range (0-255); use a validation function |
| Password strength | (?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,} | Passw0rd! | Requires lowercase, uppercase, digit, special char, min 8 chars |
| Hex color | #([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b | #ff5733, #abc | Requires # prefix |
| Time (HH:MM) | \d{2}:\d{2} | 14:30, 09:05 | Does not validate hour (0-23) or minute (0-59) ranges |
Performance Characteristics by Pattern Type
| Pattern Type | Example | Backtracking Risk | Typical Input | Execution Time |
|---|---|---|---|---|
| Literal | hello | None | Short strings | < 1 ms |
| Simple character class | \d+ | Low | Log lines | < 1 ms |
| Alternation | (cat|dog|bird) | Medium | Natural language | 1-5 ms |
| Nested quantifiers | (a+)+b | Very high (catastrophic) | "aaaaaac" | Seconds to crash |
| Lookahead chain | (?=.*[A-Z])(?=.*\d) | Low | Password strings | < 1 ms |
Pattern Length vs. Readability
| Approach | Pattern | Character Count | Readability |
|---|---|---|---|
| Simple | \d{3}-\d{2}-\d{4} | 14 | High |
| Verbose (with comments) | Not supported in JS RegExp | — | — |
| Decomposed | Build with new RegExp() parts | N/A | High |
1. Be as specific as possible. The pattern .* is flexible but expensive. Instead of .*password.*, use \bpassword\b or a more precise pattern. Specific patterns reduce false positives and execute faster. A common antipattern is using .+ where \d+ or [a-z]+ would be more appropriate.
2. Avoid catastrophic backtracking. Patterns with nested quantifiers like (a+)+b or (x+)*y can cause the regex engine to explore an exponential number of paths when the input does not match. This can freeze your application for seconds or minutes. If a regex takes suspiciously long on non-matching input, review your use of nested quantifiers and consider using atomic groups (not supported in JavaScript) or restructuring the pattern. The classic example: /(a|aa)+b/ tested against a long string of a's followed by c.
3. Test edge cases thoroughly. Always test with empty strings, very long strings, strings with special characters, strings that match partially, and strings that almost match. A regex that works on your test data may fail in production on unexpected input. Common edge cases: strings with leading/trailing whitespace, strings including Unicode characters, strings with null bytes.
4. Use raw strings or double-escape in code. In JavaScript, backslashes must be escaped in string literals: new RegExp("\\d+") matches digits. Template literals do not help here. The RegExp constructor treats the string literally, so "\\d" becomes the pattern \d. Use regex literals /\d+/ when possible to avoid escaping confusion.
5. Prefer non-capturing groups when you do not need the capture. (?:pattern) is faster and uses less memory than (pattern) because the regex engine does not store the matched text. Use capturing groups only when you need to reference the captured value later (in replacements or in code).
6. Validate, then extract. Do not rely solely on regex for security-critical validation. Regex can tell you that a string looks like an email address, but it cannot tell you whether the domain has a valid MX record or whether the address actually exists. Use regex as the first pass, then apply domain-specific validation for production systems.
7. Use online regex testers for development. While this tool is great for quick testing, consider specialized tools like regex101.com for debugging complex patterns. They provide step-by-step visualization of how the regex engine processes your input, which is invaluable for understanding and fixing performance issues.
This regex tester uses the JavaScript (ECMAScript) regular expression engine, which has several important limitations compared to other engines like PCRE (used by PHP, Python's re module) or RE2 (used by Go).
Lookbehind support. JavaScript added lookbehind assertions ((?<=...) and (?<!...)) in ES2018. Modern browsers and Node.js support them, but older browsers may not. Unlike PCRE, JavaScript lookbehinds are limited: they do not support * or + without the global flag when used with ^ anchor, and they cannot contain backreferences.
No atomic groups. JavaScript does not support atomic groups ((?>...)), which are a primary tool for preventing catastrophic backtracking. In PCRE and other engines, atomic groups tell the regex engine to never backtrack into the group once it has matched. Without them, JavaScript developers must be extra careful to avoid nested quantifier patterns.
No verbose mode. JavaScript regex does not support the x flag (extended mode) found in PCRE and Python, which allows whitespace and comments within the pattern for readability. Complex patterns in JavaScript must be built programmatically or documented separately.
No recursive patterns. Patterns that need to match nested structures (like balanced parentheses) cannot be written as a single regex in JavaScript. Use a parser for nested grammar.
Limited Unicode support. The u flag enables Unicode property escapes like \p{Emoji} and \p{Script=Cyrillic}, but support varies by browser. The \w and \d shorthand classes only match ASCII characters unless the u flag is used with Unicode property escapes.
Catastrophic backtracking is real. JavaScript regex engines do not have built-in protections against catastrophic backtracking. A poorly designed pattern can cause your Node.js server or browser tab to hang. Always test patterns with long non-matching inputs before deploying to production.
- What is the difference between greedy and lazy matching?
- Greedy quantifiers (`*`, `+`, `{n,m}`) match as much text as possible. Lazy quantifiers (`*?`, `+?`, `{n,m}?`) match as little as possible. For example, given the string `<div>text</div>`, the pattern `<.*>` matches the entire string (greedy), while `<.*?>` matches just `<div>` and `</div>` in separate matches (lazy).
- What are lookahead and lookbehind assertions?
- Lookahead (`(?=...)`) and lookbehind (`(?<=...)`) are zero-width assertions that check whether a pattern can be matched without consuming characters. A positive lookahead checks that the pattern follows the current position. A negative lookahead (`(?!...)`) checks that it does not follow. Lookbehind works the same way but checks preceding text. These are commonly used in password validation to require multiple conditions without consuming characters.
- How do I match a literal dot or other special character?
- Escape special characters with a backslash. To match a literal dot, use `\.` in your pattern (remember double escaping in string literals: `\\.`). Other characters that need escaping: `\`, `+`, `*`, `?`, `(`, `)`, `[`, `]`, `{`, `}`, `^`, `$`, `|`, `.`.
- What does the 'g' flag do and why is it important?
- The global flag `g` makes the regex find all matches instead of stopping after the first one. Without `g`, `exec()` and `match()` return only the first match. In a loop, the regex maintains `lastIndex` state, so each call to `exec()` starts from where the previous match ended. Always use the `g` flag when you want all occurrences.
- How can I improve regex performance?
- Be specific: use `\d+` instead of `.+` when matching digits. Avoid nested quantifiers like `(a+)+b`. Use non-capturing groups `(?:...)` when you don't need the captured value. Anchor patterns when possible (`^` and `$`). Keep alternation branches short and put the most common matches first. Test on realistic input sizes before deploying.
- What is catastrophic backtracking and how do I prevent it?
- Catastrophic backtracking occurs when a regex with nested quantifiers tries every possible way to match a pattern and the number of paths grows exponentially. For example, `/^(d+$s?)+$/` on a long non-matching input can take minutes. Prevent it by avoiding nested quantifiers, using possessive quantifiers (not available in JS), or limiting the scope of alternation. Restructure your pattern to guarantee unique paths.
- How do I use capture groups in replacements?
- In the replacement string, `$1`, `$2`, etc. refer to captured groups by index. `$&` refers to the full match. `$$` inserts a literal dollar sign. `$`` inserts the portion before the match, and `$'` inserts the portion after the match. Example: `str.replace(/(\w+)@(\w+)/, '$2 at $1')` swaps the username and domain.
- What is the difference between `test()` and `exec()`?
- `test()` returns a boolean indicating whether a match exists. `exec()` returns the match object with details (matched text, index, input, capture groups) or null. Use `test()` for simple existence checks, and `exec()` when you need match positions or capture groups. Both methods maintain `lastIndex` state when the regex has the `g` flag.
- How do I match across multiple lines?
- Use the `m` (multiline) flag so that `^` and `$` match the start and end of each line, not just the whole string. For the dot to match newline characters, use the `s` (dotall) flag. Combine them as `gm` or `gms` for multiline, cross-line matching.
- What Unicode property escapes are available in JavaScript?
- With the `u` flag, you can use `\p{...}` for Unicode categories: `\p{L}` for any letter, `\p{N}` for numbers, `\p{P}` for punctuation, `\p{Emoji}` for emoji, `\p{Script=Greek}` for Greek script. Negated forms use `\P{...}`. These are available in modern environments (ES2018+).
- How do I match an email address with regex?
- A common pattern is `[\w.-]+@[\w.-]+\.\w+`. However, regex alone cannot fully validate email addresses per RFC 5322. The official grammar includes comments, quoted strings, and IP address literals that are extremely complex to match. Use regex as a first pass filter, then send a confirmation email for real validation.
- What is a word boundary (`\b`) and when should I use it?
- A word boundary `\b` matches the position between a word character (`\w`) and a non-word character (`\W`). It prevents partial matches. For example, `\bcat\b` matches the word "cat" but not "caterpillar" or "scat". Use `\b` when you want to match whole words only.
- [1]MDN Web Docs: RegExp (Regular Expression)
- [2]Regular-Expressions.info — Regex Tutorial and Reference
- [3]regex101 — Online Regex Tester and Debugger
- [4]ECMA-262 — ECMAScript Language Specification (RegExp section)
- [5]RexEgg — Regex Tutorial, Performance, and Pitfalls
- [6]JavaScript.info — Regular Expressions
- [7]Debuggex — Regex Visualizer and Tester
- [8]SWITCH — Regex Testing Tool by the University of Fribourg
Last updated: June 15, 2026
UnByte — Independent Software Engineering
Every calculator references authoritative sources — Editorial policy