Regex Tester

Test and validate regular expressions online. Free regex tester with pattern matching and instant results.

Regex Flavor/Syntax Differences

⚠️ Note: This tool uses JavaScript RegEx engine. This section ONLY shows syntax differences between languages for reference. To test regex for Java/Python/etc, you need to use tools in those languages.

ECMAScript Regex - Browser & Node.js

Key Differences:

  • Lookbehind supported (ES2018+): (?<=...) and (?<!...)
  • Named groups: (?<name>...)
  • Unicode property escapes: \p{L} with 'u' flag
  • Sticky flag 'y' for position-specific matching
  • DotAll flag 's' makes . match newlines

💡 More languages (Rust, Swift, Kotlin, etc.) will be supported in the future.

Common Patterns

Email Address
Standard email validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA...
URL/Website
Match HTTP/HTTPS URLs
https?://([\w\-]+\.)+[\w\-]+(/[\w\-\./?%...
Phone (US)
US phone number format
^\+?1?\s*\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\...
Phone (Vietnam)
Vietnamese phone number format
^(\+84|0)(3[2-9]|5[6|8|9]|7[0|6-9]|8[1-9...
Phone (International)
E.164 international format
^\+?[1-9]\d{1,14}$
IPv4 Address
Valid IPv4 address
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)...
IPv6 Address
Full IPv6 address
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
Date (YYYY-MM-DD)
ISO date format
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]...
Date (DD/MM/YYYY)
European date format
^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2]...
Date (MM/DD/YYYY)
US date format
^(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01]...
Time (24-hour)
24-hour time format
^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0...
Time (12-hour)
12-hour time with AM/PM
^(0?[1-9]|1[0-2]):[0-5][0-9]\s?(AM|PM|am...
Hex Color
CSS hex color code
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
RGB Color
CSS RGB color
^rgb\(\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|2...
Username
Alphanumeric username 3-16 chars
^[a-zA-Z0-9_-]{3,16}$
Strong Password
Min 8 chars, uppercase, lowercase, digit, special
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!...
Credit Card (Visa)
Visa card number
^4[0-9]{12}(?:[0-9]{3})?$
Credit Card (Mastercard)
Mastercard number
^5[1-5][0-9]{14}$
SSN (US)
US Social Security Number
^\d{3}-\d{2}-\d{4}$
ZIP Code (US)
US ZIP code
^\d{5}(-\d{4})?$
Postal Code (UK)
UK postcode
^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$
MAC Address
Network MAC address
^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})...
HTML Tag
Match HTML tags
<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)
Slug/URL Path
URL-friendly slug
^[a-z0-9]+(?:-[a-z0-9]+)*$
Integer Number
Positive or negative integer
^-?\d+$
Decimal Number
Decimal with fraction
^-?\d+\.\d+$
Currency (USD)
US dollar amount
^\$?[0-9]{1,3}(,[0-9]{3})*\.?[0-9]{0,2}$
File Extension
Extract file extension
\.[a-zA-Z0-9]+$
Git Repository
Git repository URL
((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)...
Semantic Version
Semver format
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*...
UUID v4
UUID version 4
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[8...
JWT Token
JSON Web Token
^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z...

Quick Reference

.Any character
\dDigit (0-9)
\wWord character
\sWhitespace
^Start of line
$End of line
*0 or more
+1 or more
?0 or 1
{n,m}Between n and m

What is Regular Expression (Regex)?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. It's a powerful tool used in text processing for pattern matching, validation, search and replace operations, and data extraction. Regular expressions are supported in virtually all modern programming languages and text editors, making them an essential skill for developers, data analysts, and anyone working with text data. They use special characters (metacharacters) to create flexible patterns that can match multiple variations of text.

History of Regular Expressions

Regular expressions originated in the 1950s when mathematician Stephen Cole Kleene described regular languages using mathematical notation called regular sets. In the 1960s, Ken Thompson built Kleene's notation into the editor QED, and later into the Unix editor ed, which led to the grep utility. The concept spread through Unix utilities and eventually became a standard feature in most programming languages. In 1986, POSIX standardized regex syntax. Perl introduced powerful extensions in the late 1980s, creating what's known as Perl Compatible Regular Expressions (PCRE), which became the de facto standard and influenced regex implementations in many modern languages like Python, JavaScript, PHP, and Java.

Basic Regex Syntax

Regular expressions use special characters to define patterns. Literal characters match themselves, while metacharacters have special meanings. The most common metacharacters include: dot (.) matches any character except newline, caret (^) matches start of line, dollar ($) matches end of line, square brackets [] define character classes, parentheses () create capture groups, pipe (|) means OR, asterisk (*) means zero or more, plus (+) means one or more, question mark (?) means zero or one, and curly braces {} specify exact counts. Backslash (\) escapes special characters or introduces special sequences like \d for digits, \w for word characters, \s for whitespace, and their uppercase versions for negation.

Common Metacharacters

  • . (Dot): Matches any single character except newline
  • ^ (Caret): Matches the start of a line or string
  • $ (Dollar): Matches the end of a line or string
  • * (Asterisk): Matches 0 or more repetitions of the preceding element
  • + (Plus): Matches 1 or more repetitions of the preceding element
  • ? (Question): Matches 0 or 1 repetition (makes preceding element optional)
  • [] (Brackets): Defines a character class (matches any single character inside)
  • | (Pipe): Alternation operator (logical OR)
  • () (Parentheses): Creates a capture group and defines scope
  • {} (Braces): Specifies exact number of repetitions {n}, {n,}, {n,m}

Character Classes

  • \d: Matches any digit (0-9), equivalent to [0-9]
  • \D: Matches any non-digit character
  • \w: Matches any word character (alphanumeric + underscore), equivalent to [a-zA-Z0-9_]
  • \W: Matches any non-word character
  • \s: Matches any whitespace character (space, tab, newline)
  • \S: Matches any non-whitespace character
  • \b: Word boundary (position between \w and \W)
  • \B: Not a word boundary

Regex Flags

  • g (Global): Find all matches rather than stopping after the first match
  • i (Case-insensitive): Make the pattern case-insensitive
  • m (Multiline): ^ and $ match start/end of lines, not just start/end of string
  • s (Dotall): Dot (.) matches newline characters
  • u (Unicode): Treat pattern and string as Unicode
  • y (Sticky): Match only from the index indicated by the lastIndex property

Common Regex Patterns

Email
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
URL
https?://[\w\-\.]+(:\d+)?(/[\w\-\./?%&=]*)?
Phone (US)
^\+?1?\s*\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
IPv4 Address
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
Hex Color
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Username
^[a-zA-Z0-9_-]{3,16}$
Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Common Use Cases

  • Input Validation: Validate email addresses, phone numbers, URLs, and other user inputs
  • Data Extraction: Extract specific information from logs, documents, or web pages
  • Search and Replace: Find and replace text patterns in code editors and IDEs
  • Text Parsing: Parse structured text formats like CSV, logs, or configuration files
  • URL Routing: Match URL patterns in web frameworks for routing requests
  • Content Filtering: Filter or sanitize user input to prevent security issues

Best Practices

  • Keep It Simple: Start with simple patterns and add complexity only when needed
  • Be Specific: Use specific character classes (\d, \w) instead of broad ones (.) when possible
  • Use Anchors: Use ^ and $ to match entire strings and prevent partial matches
  • Escape Metacharacters: Remember to escape special characters when matching them literally
  • Test Thoroughly: Test your regex with various inputs including edge cases
  • Document Complex Patterns: Add comments or documentation for complex regex patterns
  • Consider Performance: Avoid catastrophic backtracking with nested quantifiers
  • Know Alternatives: Sometimes string methods are simpler and faster than regex

Performance Considerations

Regular expressions can be powerful but also computationally expensive if not written carefully. Catastrophic backtracking occurs when a regex engine tries many possible ways to match a pattern, especially with nested quantifiers like (a+)+. This can cause exponential time complexity and freeze applications. To avoid this: use possessive quantifiers when available, be careful with alternation inside repeated groups, anchor your patterns when possible, and consider using string methods for simple searches. Always test regex performance with large inputs, and use online tools to analyze and optimize complex patterns. In production systems, implement timeouts for regex operations to prevent denial-of-service vulnerabilities.

How to Use

  1. Enter Pattern: Type your regular expression pattern in the first field
  2. Set Flags: Enter flags like 'g' (global), 'i' (case-insensitive), 'm' (multiline)
  3. Enter Test Text: Paste or type the text you want to test against
  4. Test: Click 'Test Regex' to see all matches highlighted
  5. Iterate: Refine your pattern based on the results until it matches exactly what you need

Frequently Asked Questions

What's the difference between greedy and lazy quantifiers?

Greedy quantifiers (*, +, {n,}) match as much text as possible, while lazy quantifiers (*?, +?, {n,}?) match as little as possible. For example, given the string '<p>Hello</p><p>World</p>', the pattern '<.*>' (greedy) matches the entire string, but '<.*?>' (lazy) matches only '<p>' and '</p>' separately. Use lazy quantifiers when you want to match the shortest possible string, especially when working with delimiters or tags.

How do I match special characters literally in regex?

Special characters (metacharacters) like . * + ? [ ] ( ) { } ^ $ | \ have special meanings in regex. To match them literally, you must escape them with a backslash. For example, to match a literal period, use '\.' instead of '.'. To match a backslash itself, use '\\'. In character classes [], most special characters lose their special meaning, except for ^, -, ], and \.

What is catastrophic backtracking and how can I avoid it?

Catastrophic backtracking occurs when a regex engine tries exponentially many ways to match a pattern, typically with nested quantifiers like (a+)+ or (a*)*. This can cause your application to hang. To avoid it: use possessive quantifiers when available (+, *), be careful with alternation inside repeated groups, use atomic groups, anchor your patterns, and test with large inputs. Consider using string methods for simple patterns instead of complex regex.

How do capture groups work in regular expressions?

Capture groups are defined with parentheses () and allow you to extract specific parts of a match. For example, in the pattern '(\d{3})-(\d{3})-(\d{4})' applied to '123-456-7890', group 1 captures '123', group 2 captures '456', and group 3 captures '7890'. Non-capturing groups (?:...) match without saving, useful when you need grouping for precedence but don't need the captured value. Named capture groups (?<name>...) let you refer to groups by name instead of number.

What regex flags should I use and when?

Common flags include: 'g' (global) to find all matches instead of stopping at the first one, useful in search/replace operations; 'i' (case-insensitive) to match regardless of letter case; 'm' (multiline) to make ^ and $ match line boundaries instead of string boundaries; 's' (dotall) to make . match newlines; 'u' (unicode) for proper Unicode handling; 'y' (sticky) for position-specific matching. Combine flags as needed, like 'gi' for global case-insensitive matching.

How can I validate an email address with regex?

A simple email validation pattern is ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$. However, true RFC-compliant email validation is extremely complex. For production use, it's better to: use a well-tested library, send a verification email to confirm, or use a simple pattern to catch obvious errors. Remember that some valid emails might be rejected by overly strict patterns, and some invalid ones might pass simple checks.

What's the difference between .* and .+ in regex?

Both are quantifiers applied to the dot (.) which matches any character. The asterisk (*) means 'zero or more', so .* can match an empty string or any number of characters. The plus (+) means 'one or more', so .+ must match at least one character. Use .* when the pattern is optional, and .+ when at least one character is required. Both are greedy by default; add ? to make them lazy (.*? or .+?).

Can I use regex for parsing HTML or XML?

While regex can match simple HTML/XML patterns, it's not recommended for parsing structured markup. HTML/XML are context-free languages that require a proper parser to handle nested tags, attributes, and edge cases correctly. Use regex for simple extraction tasks or quick text processing, but use a dedicated parser (like DOMParser, BeautifulSoup, or XML parsers) for reliable parsing. The famous Stack Overflow answer about parsing HTML with regex warns against it for good reasons!

Related Tools