Regex Tester
Test and debug regular expressions.
A regular expression (regex) is a pattern of characters that defines a search rule โ used in every programming language to match, validate, extract, or replace text. To test a regex online: enter your pattern in the Pattern field, set flags like g (global) or i (case-insensitive) in the Flags field, paste your test string below, and matches are highlighted in real time. Common use cases include email and phone number validation, log file parsing, form input checking, and find-and-replace in code editors. All testing runs locally in your browser โ no text is uploaded or stored.
What is Regex?
Regular Expressions (Regex) are sequences of characters that define a search pattern. They are incredibly powerful for searching, validating, editing, and manipulating text across all major programming languages including JavaScript, Python, Java, PHP, and Go. A regex pattern like \d{3}-\d{4} matches any phone suffix in the format "555-1234", while ^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$ validates an email address. The real-time tester above shows every match highlighted as you type, so you can debug patterns instantly without writing any code.
Common Regex Patterns
- Email Validation:
^[^\s@]+@[^\s@]+\.[^\s@]+$ - Phone Numbers:
^\d{3}-\d{3}-\d{4}$(US Format) - Dates:
^\d{4}-\d{2}-\d{2}$(YYYY-MM-DD)
Why Use This Tester?
Writing Regex can be difficult and error-prone. This real-time tester allows you to instantly see what your pattern matches as you type, helping you debug and refine your expressions quickly and safely in your browser.
Example: Extract Emails
Pattern
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}
Sample Text
Contact us at hello@example.com or support@example.org
Best Practices
- Test against both valid and invalid samples to avoid false positives.
- Use anchors (
^and$) when you need full-string validation. - Start with a simple pattern, then refine it incrementally.
Flag Reference
- g: Global search across the entire input.
- i: Case-insensitive matching.
- m: Multiline mode for
^and$. - s: Dot matches newline in modern browsers.
Common Pitfalls
- Greedy matching: Use
?for lazy matches when needed. - Unescaped characters: Escape
.,+, and?when literal. - Overly broad patterns: Use character classes or anchors to tighten matches.
Suggested Workflow
- Paste a small representative sample input.
- Write the simplest pattern that matches the sample.
- Add negative samples and refine until false positives disappear.
Additional Examples
- US Phone:
^\d{3}-\d{3}-\d{4}$ - Date (YYYY-MM-DD):
^\d{4}-\d{2}-\d{2}$ - Slug:
^[a-z0-9-]+$
Performance Notes
Some patterns can be slow on large inputs due to backtracking. If the tester feels slow, try tightening your pattern or reducing the input size while you iterate.
Quick Debugging Tips
- Use small sample strings to isolate matches.
- Add anchors to ensure whole-string validation.
- Test with edge cases like empty strings and long inputs.
Troubleshooting
- No matches: Check flags like
ifor case-insensitive matching. - Too many matches: Use anchors or tighten your character classes.
- Groups not captured: Ensure parentheses are not inside a non-capturing group.
Privacy
Your regex patterns and test strings are processed locally in your browser and are not stored.
Understanding Regex Syntax
Character Classes
Character classes define sets of characters to match. \d matches any digit (0-9), \w matches word characters (letters, digits, underscore), \s matches whitespace (spaces, tabs, newlines). Their uppercase versions negate: \D (non-digits), \W (non-word chars), \S (non-whitespace).
Custom classes: [abc] matches a, b, or c. [a-z] matches any lowercase letter. [^abc] matches anything except a, b, or c. Example: [0-9a-fA-F] matches hexadecimal digits.
Quantifiers
Quantifiers specify how many times a pattern repeats. * = 0 or more, + = 1 or more, ? = 0 or 1 (optional). {n} = exactly n times, {n,} = n or more, {n,m} = between n and m times.
Greedy vs Lazy: By default, quantifiers are greedy (match as much as possible). Adding ? makes them lazy: .*? matches minimum characters, .+? matches minimum 1+ characters. Example: <.*?> matches first tag, <.*> matches from first < to last >.
Anchors and Boundaries
Anchors match positions, not characters. ^ matches start of string/line, $ matches end of string/line. \b matches word boundary (between \w and \W), \B matches non-word boundary.
Example: ^\d{3}$ matches strings of exactly 3 digits. \bword\b matches "word" as a whole word but not in "password". Without anchors, patterns match anywhere in the string.
Groups and Capturing
Capturing groups (...) remember matched text for extraction. (abc)+ matches one or more "abc" sequences and captures the last one. Use \1, \2 to reference captured groups (backreferences).
Non-capturing groups (?:...) group without capturing, improving performance. (?:abc)+ matches but doesn't store. Named groups (?<name>...) allow referring by name instead of number.
Alternation and Special Characters
Alternation | works like OR. (cat|dog) matches "cat" or "dog". (jpg|jpeg|png) matches any of three extensions. Combine with groups to control scope.
Special characters need escaping with backslash to match literally: \. for period, \* for asterisk, \+ for plus, \? for question mark, \\ for backslash, \( and \) for parentheses.
Real-World Pattern Examples
Email Validation (Simple)
Pattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Breakdown: ^[a-zA-Z0-9._%+-]+ = username (letters, numbers, dots, underscores, percent, plus, hyphen). @ = literal at sign. [a-zA-Z0-9.-]+ = domain (letters, numbers, dots, hyphens). \.[a-zA-Z]{2,}$ = dot + 2+ letter TLD (com, org, info).
Matches: user@example.com, first.last@company.org, user+tag@domain.co.uk
Limitation: Simplified version. Real RFC 5322 email validation is extremely complex with many edge cases.
Phone Numbers (US Format)
Pattern: ^\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$
Breakdown: \(? = optional opening parenthesis. (\d{3}) = 3-digit area code (captured). \)? = optional closing parenthesis. [-.\s]? = optional separator (hyphen, dot, or space). Second (\d{3}) = 3-digit exchange. Final (\d{4}) = 4-digit number.
Matches: (555) 123-4567, 555-123-4567, 555.123.4567, 5551234567
Captures: Three groups for area code, exchange, and number for easy extraction or reformatting.
URL Extraction
Pattern: https?://[^\s/$.?#].[^\s]*
Breakdown: https? = http or https. :// = literal separator. [^\s/$.?#]. = domain starting character + at least one more character. [^\s]* = rest of URL (anything except whitespace).
Matches: https://example.com, http://site.org/path?query=value
Use Case: Extract URLs from log files, chat messages, or text documents with g flag for global matching.
Date Formats (ISO 8601)
Pattern: ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Breakdown: \d{4} = 4-digit year. (0[1-9]|1[0-2]) = month 01-12 (01-09 or 10-12). (0[1-9]|[12]\d|3[01]) = day 01-31 (01-09, 10-29, or 30-31).
Matches: 2024-01-15, 2024-12-31
Note: Validates format but not logic (e.g., allows 2024-02-31 which doesn't exist). For full date validation, use Date parsing in code after regex match.
Hex Color Codes
Pattern: ^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Breakdown: ^#? = optional hash symbol. ([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ = either 3 or 6 hex digits (0-9, a-f, A-F).
Matches: #FFF, #ffffff, ABC, #1a2b3c
Use Case: Validate CSS color codes in stylesheets or design tools.
Credit Card Numbers (Simple)
Pattern: ^\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}$
Breakdown: Four groups of 4 digits \d{4}, each optionally separated by hyphen or space [-\s]?.
Matches: 1234-5678-9012-3456, 1234 5678 9012 3456, 1234567890123456
Security Note: Only validates format. Use Luhn algorithm for actual validation. Never log or store credit card numbers in plain text.
Common Use Cases
1. Form Validation
Validate user input in real-time before form submission. Email format, password strength (min 8 chars, 1 uppercase, 1 number: ^(?=.*[A-Z])(?=.*\d).{8,}$), username rules (alphanumeric + underscore: ^[a-zA-Z0-9_]{3,16}$), phone numbers, postal codes, credit cards.
2. Log File Analysis
Extract structured data from logs. Find all error messages: ERROR.*. Extract IP addresses: \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b. Parse timestamps: \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}. Filter HTTP status codes: (4\d{2}|5\d{2}) (400-599 errors).
3. Text Editing and Search/Replace
Bulk text transformations in code editors (VS Code, Sublime Text, Vim). Rename variables: find \boldName\b, replace with newName. Convert date formats: find (\d{2})/(\d{2})/(\d{4}) (MM/DD/YYYY), replace with $3-$1-$2 (YYYY-MM-DD). Remove trailing whitespace: \s+$.
4. Data Extraction and Web Scraping
Extract specific data from HTML, JSON, or text files. Find all email addresses in a document with g flag. Extract product prices: \$\d+\.\d{2}. Parse CSV fields: (?:^|,)("(?:[^"]|"")*"|[^,]*). Extract URLs from markdown links: \[.*?\]\((https?://[^\)]+)\).
5. Code Review and Refactoring
Find code patterns needing refactoring. Locate hardcoded values: ("|')[\w\s]+\1 (quoted strings). Find old API calls: oldApi\.method\([^)]*\). Identify TODO comments: //\s*TODO:.*. Detect potential SQL injection: execute.*\+.*input.
6. Security and Privacy
Scan code for sensitive data leaks. Find potential API keys: [A-Za-z0-9]{32,} (long alphanumeric strings). Detect credit card patterns: \b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b. Locate hardcoded passwords: password\s*=\s*["'][^"']+["']. Search for SSN patterns: \b\d{3}-\d{2}-\d{4}\b.
Advanced Regex Techniques
Lookaheads and Lookbehinds
Positive lookahead (?=...) matches if pattern ahead exists without consuming characters. \d+(?= dollars) matches numbers only when followed by " dollars".
Negative lookahead (?!...) matches if pattern ahead doesn't exist. \d+(?! dollars) matches numbers NOT followed by " dollars".
Lookbehind (?<=...) (positive) and (?<!...) (negative) check patterns before current position. (?<=\$)\d+ matches numbers preceded by dollar sign. Limited browser support for lookbehinds.
Password Strength Validation
Complex requirements: Minimum 8 characters, 1 uppercase, 1 lowercase, 1 digit, 1 special char.
Pattern: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Uses four lookaheads to enforce each requirement independently, then validates character set and length. Each (?=.*...) checks entire string for requirement without consuming characters.
Non-Greedy Matching
Problem: <.*> matches from first < to last > in <div>content</div>, capturing entire string.
Solution: <.*?> matches minimum characters, capturing <div> and </div> separately.
Add ? after any quantifier (*?, +?, {n,}?) for lazy matching. Critical for HTML/XML parsing and extracting content between delimiters.
Troubleshooting Common Issues
Issue: Pattern Doesn't Match Expected Text
Causes: Case sensitivity (use i flag), anchors too restrictive (remove ^/$ for partial matches), special characters not escaped.
Debug approach: Start with simplest pattern that matches anything, then incrementally add constraints. Test with minimal input. Use online debuggers like regex101.com to visualize matches.
Issue: Too Many Matches (False Positives)
Causes: Pattern too broad (.* matches everything), missing anchors, character classes too permissive.
Solutions: Add anchors ^/$ for full-string validation. Tighten character classes: use [a-z] instead of .. Add word boundaries \b. Test against negative samples.
Issue: Catastrophic Backtracking (Slow Performance)
Cause: Nested quantifiers cause exponential backtracking. Example: (a+)+b on "aaaaaaaaac" tries billions of combinations.
Solutions: Avoid nested quantifiers. Use atomic groups or possessive quantifiers where supported. Simplify pattern: replace (.*)* with .*. Test with long inputs to identify performance issues early.
Issue: Special Characters Not Matching Literally
Problem: example.com matches "exampleXcom" because . means "any character".
Solution: Escape special characters: example\.com. Special chars needing escaping: . * + ? ^ $ { } [ ] ( ) | \. Use \Q...\E for literal strings with many special chars (limited support).
Issue: Multiline Text Not Matching
Cause: ^ and $ only match string start/end by default. . doesn't match newlines.
Solutions: Use m flag for multiline mode (^/$ match line boundaries). Use s flag (dotAll) so . matches newlines. Alternative: use [\s\S] instead of . to match any character including newlines.
Issue: Capture Groups Not Working
Causes: Using non-capturing groups (?:...) by mistake. Backreferences incorrect (\1 refers to first group). Global flag g with JavaScript exec() requires looping.
Solutions: Use (...) for capturing, not (?:...). Verify group numbering (left-to-right by opening parenthesis). Access captures via match array or named groups (?<name>...).
Regex in Different Programming Languages
JavaScript
Syntax: /pattern/flags or new RegExp('pattern', 'flags'). Methods: test(), exec(), match(), replace(), search(), split().
Example: const regex = /\d+/g; "a1b2c3".match(regex); // ["1", "2", "3"]
Note: This tester uses JavaScript regex engine, so results match browser/Node.js behavior.
Python
Module: import re. Methods: re.match(), re.search(), re.findall(), re.sub(), re.split().
Example: re.findall(r'\d+', 'a1b2c3') # ['1', '2', '3']
Differences: Python uses raw strings r'...' to avoid double escaping. Named groups: (?P<name>...). Inline flags: (?i) for case-insensitive.
Java
Classes: Pattern and Matcher. Pattern.compile("regex").matcher("text").find().
Example: Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher("a1b2"); while(m.find()) { System.out.println(m.group()); }
Note: Backslashes must be double-escaped in Java strings: \\d instead of \d.
PHP
Functions: preg_match(), preg_match_all(), preg_replace(), preg_split(). Uses PCRE (Perl-Compatible Regular Expressions).
Example: preg_match_all('/\d+/', 'a1b2c3', $matches); // $matches[0] = ['1', '2', '3']
Features: Supports many advanced features like lookbehinds, atomic groups, possessive quantifiers.
Testing Best Practices
- Start simple, iterate: Begin with basic pattern, test, then add constraints. Don't try to write perfect regex in one attempt.
- Test positive and negative cases: Verify pattern matches valid inputs AND rejects invalid ones. False positives are as bad as false negatives.
- Use anchors appropriately:
^...$for full-string validation (forms). No anchors for finding patterns within text (search/extract). - Consider edge cases: Empty strings, very long inputs, special characters, Unicode, leading/trailing spaces.
- Document complex patterns: Add comments explaining pattern sections. Use online tools like regex101.com for documentation and visualization.
- Benchmark performance: Test with large inputs to catch catastrophic backtracking. Optimize hot paths in production code.
- Validate externally: For critical patterns (email, security), use well-tested libraries instead of custom regex.
- Use named groups:
(?<year>\d{4})improves readability over numbered groups, especially in long patterns.
Privacy and Security
All regex testing happens entirely in your browser using JavaScript. Your patterns and test strings never leave your deviceโno data is uploaded, stored, or transmitted. This client-side processing ensures complete privacy for sensitive data like customer records, passwords, or proprietary text.
Security note: Be cautious testing patterns with real passwords, API keys, or PII (Personally Identifiable Information). While the tool doesn't store data, browser history or autocomplete may cache input fields. For highly sensitive data, use private/incognito browsing mode.
Frequently Asked Questions
Which regex flavor is supported?
This tester uses JavaScript regex (ECMAScript) behavior, which is the same engine in Chrome, Firefox, Safari, Edge, and Node.js. It supports most common features: character classes, quantifiers, groups, lookaheads, anchors, flags (g, i, m, s, u, y). However, it has limited lookbehind support (only in modern browsers) and no possessive quantifiers or atomic groups. For advanced features like conditional expressions or subroutines, use PCRE-based tools (PHP, Python regex module with VERBOSE flag).
Can I test multiline patterns?
Yes, use the m flag (multiline mode) to make ^ and $ match at line boundaries instead of only string start/end. Example: With m flag, ^\d+ matches numbers at the start of each line in multiline text. Additionally, use the s flag (dotAll) to make . match newline characters, or use [\s\S] as an alternative that works in older browsers.
Does this tool store my text?
No, absolutely not. All regex testing happens locally in your browser using JavaScript. Your patterns and test strings are never uploaded to servers, stored in databases, or transmitted over the network. The tool has no backendโit's purely client-side code. However, browser autocomplete or history might cache input fields, so use private/incognito mode for highly sensitive data.
Why is my pattern slow or freezing the browser?
This is likely catastrophic backtrackingโa performance issue caused by nested quantifiers or ambiguous patterns. Example: (a+)+b tested against "aaaaaaaaaac" (no 'b' at end) causes exponential backtracking as the regex engine tries every possible way to split the a's. Solutions: Simplify pattern, avoid nested quantifiers ((a+)+ โ a+), use atomic groups where supported, or test with shorter input strings first. For production, use regex profilers to identify slow patterns.
How do I match special characters literally?
Escape special characters with backslash: \. for period, \* for asterisk, \+ for plus, \? for question mark, \\ for backslash, \( and \) for parentheses, \[ and \] for brackets, \{ and \} for braces, \^ for caret, \$ for dollar, \| for pipe. Inside character classes [...], most special chars lose meaning (except ^, -, ]).
What's the difference between match() and test()?
In JavaScript: regex.test(string) returns true/false (does pattern exist in string?). string.match(regex) returns array of matches (with g flag) or array with captures (without g flag), or null if no match. Use test() for validation (yes/no), match() for extraction (get the actual matched text). This tester shows both match highlights and extracted groups.
How do I extract multiple values from each match?
Use capturing groups (...) in your pattern. Example: (\d{3})-(\d{3})-(\d{4}) captures three groups from phone number. In JavaScript, use exec() in a loop with g flag, or matchAll() in modern browsers: for (const match of string.matchAll(regex)) { console.log(match[1], match[2], match[3]); }. Named groups (?<area>\d{3}) allow accessing via match.groups.area.
Can I use regex to parse HTML or JSON?
Short answer: Don't. HTML and JSON have nested structures that regex cannot properly parse. Regex may work for simple extractions but breaks with nesting, comments, escaped characters, or edge cases. For HTML: Use DOM parsers (browser's DOMParser, jsdom, cheerio). For JSON: Use JSON.parse(). Regex is fine for finding simple patterns like URLs in HTML or extracting values from flat JSON, but not for parsing the entire structure.
What are lookaheads and when should I use them?
Lookaheads (?=...) (positive) and (?!...) (negative) check if a pattern exists ahead without consuming characters. Use for: (1) Password validation with multiple requirementsโ^(?=.*[A-Z])(?=.*\d).{8,}$ checks uppercase and digit exist anywhere. (2) Conditional matchingโ\d+(?= dollars) matches numbers only if followed by " dollars". (3) Complex validation requiring multiple checks on same text. Lookbehinds (?<=...) check patterns behind current position (limited browser support).
How do I make my regex case-insensitive?
Add the i flag (ignore case) to your regex. In JavaScript: /pattern/i or new RegExp('pattern', 'i'). In this tester, enter i in the Flags field. Example: /hello/i matches "hello", "Hello", "HELLO", "HeLLo". The i flag applies to entire patternโfor case-insensitive matching of only part of pattern, use character classes: [Hh][Ee][Ll][Ll][Oo].
Can I test Unicode characters and emoji?
Yes, use the u flag (Unicode mode) for proper Unicode support. Without u flag, emoji and characters outside Basic Multilingual Plane may not match correctly. Example: /\u{1F600}/u matches ๐ emoji. /\p{Emoji}/u matches any emoji (in modern browsers). \w with u flag matches Unicode letters, not just ASCII. For international text, always use u flag to avoid unexpected behavior with accented characters or non-Latin scripts.
Practical Guide
Use this checklist to get reliable results from Regex Tester and avoid common errors.
Input Checklist
- Test against both valid and invalid examples.
- Confirm whether your pattern expects multiline input.
- Paste a small representative sample first, then expand.
How to Get Better Results
- Start with a representative sample in Regex Tester and validate one test run first.
- Start with a minimal sample input, then expand gradually to cover edge cases.
- Validate output formatting before copy/paste into production configs or pipelines.
- Keep one clean baseline sample to speed up debugging when regressions appear.
Expected Output Checklist
- Clean output suited for copy/paste into APIs, scripts, and pull requests.
- A clearer structure that reduces debugging time during implementation.
- Consistent formatting that improves review quality across teams.