RegEx Tester: Online Tool for Testing Regular Expressions
Complete guide to RegEx Tester: test regular expressions in real-time. Common patterns (email, phone, SSN), JavaScript/Python/PHP examples and best practices.
RegEx Tester: Online Tool for Testing Regular Expressions
Regular expressions (regex) are character sequences that define search patterns in text. Essential for developers, data analysts, and anyone working with text processing, regex enables input validation, data extraction, and advanced search and replace operations.
Our online RegEx Tester is completely free, fast, and privacy-first: it processes everything in the browser without sending data to external servers. Ideal for testing, debugging, and perfecting your regular expressions in real-time.
What is a Regular Expression
A regular expression (regex or regexp) is a pattern that describes a set of text strings. Instead of searching for an exact string, a regex defines general rules that the text must follow.
Basic Example
// Search for exact string
"hello" โ finds only "hello"
// Regex with pattern
/hel+o/ โ finds "helo", "hello", "helllo", "hellllo"...
/h[ae]llo/ โ finds "hallo" and "hello"
/\d{3}-\d{4}/ โ finds "123-4567" (phone format)
Fundamental Components
- Literal characters: `abc` searches exactly "abc"
- Metacharacters: `.` (any), `*` (zero or more), `+` (one or more), `?` (optional)
- Character classes: `[abc]` (a or b or c), `[0-9]` (digit), `[a-z]` (lowercase letter)
- Anchors: `^` (start), `$` (end), `\b` (word boundary)
- Groups: `(abc)` (capture group), `(?:abc)` (non-capture group)
- Quantifiers: `{3}` (exactly 3), `{2,5}` (2 to 5), `{3,}` (3 or more)
Regular Expression Use Cases
1. User Input Validation
Verify that entered data matches a specific format:
// Email
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
// US Phone number
/^(\+1|1)?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/
// US ZIP code
/^\d{5}(-\d{4})?$/
// Credit card (basic pattern)
/^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$/
// Social Security Number (SSN)
/^\d{3}-\d{2}-\d{4}$/
// IP Address (IPv4)
/^(\d{1,3}\.){3}\d{1,3}$/
Practical case: Registration form validating email, phone, and ZIP code before server submission.
2. Data Extraction from Text
Extract structured information from unstructured text:
// Extract all email addresses from a document
const text = "Contact us: info@example.com or support@test.com"
const emails = text.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g)
// โ ["info@example.com", "support@test.com"]
// Extract dollar amounts
const prices = text.match(/\$\s?\d+[.,]?\d*/g)
// Extract dates in MM/DD/YYYY format
const dates = text.match(/\d{2}\/\d{2}\/\d{4}/g)
Practical case: Parsing log files to extract timestamps, IP addresses, error codes.
3. Advanced Search and Replace
Modify text intelligently using capture groups:
// Convert dates from MM/DD/YYYY to YYYY-MM-DD
const text = "Expiry: 12/25/2024"
text.replace(/(\d{2})\/(\d{2})\/(\d{4})/g, '$3-$1-$2')
// โ "Expiry: 2024-12-25"
// Anonymize phone numbers
text.replace(/\d{3}[\s.-]\d{3}[\s.-]\d{4}/g, 'XXX-XXX-XXXX')
// Format SSN (add dashes)
"123456789".replace(/(\d{3})(\d{2})(\d{4})/, '$1-$2-$3')
// โ "123-45-6789"
Practical case: Text editor that automatically converts date formats, normalizes phone numbers, censors sensitive data.
4. Configuration File Parsing and Validation
Read and validate structured formats (CSV, INI, env):
// Parse .env file
KEY=value โ /^([A-Z_]+)=(.*)$/
// Validate CSV row
"name,surname,email" โ /^[^,]+,[^,]+,[^,]+$/
// Extract variables from template
"Hello {{name}}, welcome!" โ /\{\{(\w+)\}\}/g
5. Web Scraping and Data Mining
Extract structured data from HTML, XML, embedded JSON:
// Extract URLs from tags
/
6. Security and Input Sanitization
Detect and block dangerous patterns:
// Detect SQL injection
/(\bOR\b|\bAND\b|--|;|\/\*|\*\/|xp_|sp_)/i
// Detect XSS
/
7. Log Analysis and Monitoring
Filter and analyze system logs, web server logs, application logs:
// Apache/Nginx access log
/^(\S+) \S+ \S+ \[([\w:\/]+\s[+\-]\d{4})\] "(\S+)\s?(\S+)?\s?(\S+)?" (\d{3})/
// Extract errors from application log
/ERROR.*Exception: (.+)/
// Performance monitoring (response times > 1000ms)
/response_time: (\d{4,})/
How to Use THEJORD RegEx Tester
Main Interface
- Open the RegEx Tester
- RegEx Pattern: Enter your regular expression (e.g., `/\d{3}-\d{4}/`)
- Flags: Select options:
g(global): Find all occurrencesi(case-insensitive): Ignore casem(multiline): `^` and `$` match line start/ends(dotAll): `.` matches newlines toou(unicode): Full Unicode support
- Test String: Enter the text to test
- See real-time results with match highlighting
Advanced Features
- Match Groups: View capture groups `(...)` extracted
- Replace Mode: Test replacements with `$1`, `$2` for groups
- Pattern Library: Predefined templates for common cases (email, URL, phone, SSN)
- Debug Mode: Visualize step-by-step how the regex matches text
- Performance: Warning if regex is inefficient (ReDoS risk)
Practical Example: Validate Email
// 1. Enter pattern
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
// 2. Flags: none (case-sensitive match)
// 3. Test string
john.doe@example.com
JANE@EXAMPLE.COM
invalid-email
test@subdomain.example.co.uk
// 4. Results
โ
john.doe@example.com (full match)
โ
JANE@EXAMPLE.COM (full match)
โ invalid-email (no match)
โ
test@subdomain.example.co.uk (full match)
Code Examples
JavaScript
// Test regex
const regex = /^\d{3}-\d{3}-\d{4}$/
regex.test('555-123-4567') // true
regex.test('555.123.4567') // false
// Extraction with match()
const text = "Email: test@example.com, info@test.com"
const emails = text.match(/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/gi)
console.log(emails) // ["test@example.com", "info@test.com"]
// Replace with capture groups
const date = "12/25/2024"
const formatted = date.replace(/(\d{2})\/(\d{2})\/(\d{4})/, '$3-$1-$2')
console.log(formatted) // "2024-12-25"
// Form validation in React
function validateEmail(email) {
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
return regex.test(email)
}
// Extract groups with exec()
const regex = /(\d{2})\/(\d{2})\/(\d{4})/
const match = regex.exec("Date: 12/25/2024")
if (match) {
console.log(`Month: ${match[1]}, Day: ${match[2]}, Year: ${match[3]}`)
}
Python
import re
# Test regex
pattern = r'^\d{3}-\d{3}-\d{4}$'
re.match(pattern, '555-123-4567') # Match object
re.match(pattern, '555.123.4567') # None
# Extraction
text = "Email: test@example.com, info@test.com"
emails = re.findall(r'[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}', text, re.IGNORECASE)
print(emails) # ['test@example.com', 'info@test.com']
# Replace with groups
date = "12/25/2024"
formatted = re.sub(r'(\d{2})/(\d{2})/(\d{4})', r'\3-\1-\2', date)
print(formatted) # "2024-12-25"
# SSN validation
def validate_ssn(ssn):
pattern = r'^\d{3}-\d{2}-\d{4}$'
return bool(re.match(pattern, ssn))
# Log parsing with named groups
log = '192.168.1.1 - - [10/Dec/2024:13:55:36] "GET /api/users HTTP/1.1" 200'
pattern = r'(?P\S+).*\[(?P[\w:/]+).*\] "(?P\w+) (?P\S+).*" (?P\d{3})'
match = re.search(pattern, log)
print(match.groupdict())
# {'ip': '192.168.1.1', 'date': '10/Dec/2024:13:55:36', 'method': 'GET', 'path': '/api/users', 'status': '200'}
PHP
<?php
// Test regex
$pattern = '/^\d{3}-\d{3}-\d{4}$/';
preg_match($pattern, '555-123-4567'); // 1 (true)
preg_match($pattern, '555.123.4567'); // 0 (false)
// Extraction
$text = "Email: test@example.com, info@test.com";
preg_match_all('/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i', $text, $matches);
print_r($matches[0]); // Array(['test@example.com', 'info@test.com'])
// Replace
$date = "12/25/2024";
$formatted = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3-$1-$2', $date);
echo $formatted; // "2024-12-25"
// SSN validation
function validateSSN($ssn) {
return preg_match('/^\d{3}-\d{2}-\d{4}$/', $ssn);
}
// Input sanitization (remove non-alphanumeric)
$input = "Hello, World! 123";
$clean = preg_replace('/[^a-zA-Z0-9\s]/', '', $input);
echo $clean; // "Hello World 123"
?>
Common RegEx Patterns (US/International)
| Type | Pattern | Example Match |
|---|---|---|
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ |
john@example.com | |
| US Phone | /^(\+1|1)?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/ |
(555) 123-4567 |
| US ZIP Code | /^\d{5}(-\d{4})?$/ |
90210, 90210-1234 |
| SSN | /^\d{3}-\d{2}-\d{4}$/ |
123-45-6789 |
| Credit Card | /^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$/ |
1234 5678 9012 3456 |
| IPv4 Address | /^(\d{1,3}\.){3}\d{1,3}$/ |
192.168.1.1 |
| URL | /^https?:\/\/[a-z0-9.-]+\.[a-z]{2,}/i |
https://example.com |
| Date MM/DD/YYYY | /^(0[1-9]|1[012])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/ |
12/25/2024 |
| Hex Color | /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/ |
#FF5733, #F00 |
| Username | /^[a-zA-Z0-9_-]{3,16}$/ |
john_doe123 |
RegEx Tester Comparison
| Tool | Highlights | Replace | Groups | Performance Warning | Library |
|---|---|---|---|---|---|
| THEJORD | โ | โ | โ | โ | โ Multi-region |
| Regex101 | โ | โ | โ | โ | โ Multi-lang |
| RegExr | โ | โ | โ | โ | โ |
| RegexPal | โ | โ | โ | โ | โ |
When to Use THEJORD RegEx Tester
- โ Privacy-first: No data sent to external servers (all local processing)
- โ Multi-region patterns: Library with regex for US, EU, international formats
- โ Fast: No loading, instant processing
- โ Free: No usage limits or registration required
- โ Multi-language: Supports JavaScript, Python, PHP, Java syntax
Best Practices and Performance
โ ๏ธ Avoid Catastrophic Backtracking (ReDoS)
Some regex can cause exponential execution time on specific input:
// โ DANGEROUS - ReDoS vulnerability
/(a+)+b/
/(\w+\s?)*$/
// โ
SAFE - Non-nested quantifiers
/a+b/
/[\w\s]+$/
Rule: Avoid nested quantifiers `(a+)+`, `(a*)*`, `(a+)*`.
โ Optimize Regex
- Use anchors: `^` and `$` limit matching scope
- Specific classes: `\d` instead of `[0-9]`, `\w` instead of `[a-zA-Z0-9_]`
- Non-capture groups: `(?:...)` instead of `(...)` if extraction not needed
- Efficient alternation: `(jpg|png|gif)` instead of `(jpg)|(png)|(gif)`
๐งช Test Edge Cases
Always verify with boundary input:
- Empty string: `""`
- Single character: `"a"`
- Special characters: `"!@#$%"`
- Unicode/emoji: `"hello ๐"`
- Very long: 10,000+ characters
- Newlines and tabs: `"hello\nworld\t"`
Frequently Asked Questions (FAQ)
What's the difference between /g and without g flag?
Without `g`, regex stops at the first match. With `g` (global), it finds all occurrences in the text.
const text = "cat bat rat"
text.match(/at/) // ["at"] (only first match)
text.match(/at/g) // ["at", "at", "at"] (all matches)
How do I match a special character like . or *?
Use backslash `\` to escape metacharacters:
/\./ โ matches literal dot "."
/\*/ โ matches asterisk "*"
/\+/ โ matches plus "+"
/\(/ โ matches parenthesis "("
Regex for strong password validation?
// Min 8 chars, at least: 1 uppercase, 1 lowercase, 1 number, 1 special
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
Explanation: Uses positive lookahead `(?=...)` to verify each condition without consuming characters.
How to extract content between HTML tags?
โ ๏ธ Warning: Regex is NOT suitable for complex HTML parsing. For HTML use a parser (DOMParser, Cheerio, BeautifulSoup).
For simple cases:
// Extract text in
/
(.*?)<\/p>/gs
// Extract href from
/
Difference between .* and .*? (greedy vs lazy)?
.*(greedy): Matches as much as possible.*?(lazy): Matches as little as possible
Input: "hello
world
"
/.*<\/p>/ โ match: "
hello
world
" (entire)
/.*?<\/p>/ โ match: "
hello
" (only first tag)
Can I use regex to validate email 100%?
No. RFC 5322 (email standard) allows very complex cases. The complete regex is 6000+ characters long. For production:
- โ
Use simple regex for basic validation:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/ - โ Send verification email for real confirmation
- โ Don't seek the "perfect regex" for email
Are regex case-sensitive?
By default yes. Use the `i` flag to ignore case:
/hello/.test("HELLO") // false
/hello/i.test("HELLO") // true
Related Resources
Explore other useful THEJORD tools:
- Diff Checker: Compare texts and verify differences between regex patterns
- JSON Formatter: Validate JSON with regex for keys/values
- Hash Generator: Generate hashes to verify integrity of extracted patterns
- URL Encoder: Encode URLs extracted with regex