Free RegEx Tester β€” Test Regular Expressions Online

Enter a regular expression pattern, choose your flags, and paste a test string. Matches are highlighted live. A library of common patterns is included for quick reference. Runs entirely in your browser.

Advertisement Β· 728Γ—90
/ / g

What Is a Regular Expression?

A regular expression (abbreviated regex or regexp) is a sequence of characters that defines a search pattern. Regular expressions are used to search, match, extract, and replace text based on pattern rules rather than fixed strings. They are supported natively in virtually every programming language β€” JavaScript, Python, Java, Go, Ruby, PHP, Perl, Swift β€” as well as in command-line tools like grep, sed, and awk, and in text editors like VS Code, Sublime Text, and Vim.

The power of regex comes from its conciseness: a single pattern can describe an infinite set of matching strings. For example, the pattern \d{3}-\d{4} matches any string that has exactly three digits, a hyphen, and then four digits β€” covering every US phone number suffix in one compact expression. Learning to write and read regular expressions is one of the most high-leverage skills a developer or data analyst can acquire, enabling text processing tasks that would otherwise require hundreds of lines of procedural code.

Common Regex Patterns Explained

Email addresses: A basic email pattern like [a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} matches the local part (before the @), the @ symbol, the domain name, and a top-level domain of at least two characters. Real email validation is significantly more complex per the RFC 5321 standard, but this pattern catches the vast majority of valid email addresses used in practice.

URLs: URL patterns match the protocol (https?), an optional www., one or more domain segments, and an optional path. URLs can be complex because they may contain query strings, fragments, ports, and percent-encoded characters. The pattern in this tool matches most common web URLs reliably.

IP addresses: An IPv4 address consists of four octets (0–255) separated by dots. The pattern uses alternation to match each valid octet range: 250–255, 200–249, 100–199, 10–99, and 0–9. This is a classic regex challenge that illustrates how regex can encode numeric range constraints.

Dates (YYYY-MM-DD): This pattern matches ISO 8601 date strings and validates month (01–12) and day (01–31) ranges to a reasonable degree, though it does not account for calendar-specific rules like February having 28 or 29 days.

Password strength: The pattern uses lookaheads to require at least one uppercase letter, one lowercase letter, one digit, one special character, and a minimum length of 8 characters β€” all without specifying the order of characters. Lookaheads are a powerful zero-width assertion that check for a condition without consuming characters.

Regex Flags and Their Effects

Regex flags modify how the pattern engine interprets and applies the pattern. The g (global) flag finds all matches rather than stopping at the first. Without g, String.prototype.match() in JavaScript returns only the first match. The i (case insensitive) flag makes the pattern match regardless of letter case, so /hello/i matches "Hello", "HELLO", and "hello". The m (multiline) flag changes the meaning of ^ and $: without m, they match the start and end of the entire string; with m, they match the start and end of each line (separated by newlines). The s (dotall) flag makes the dot (.) match any character including newlines; by default, . does not match \n.

Regex in JavaScript, Python, and Other Languages

In JavaScript, regex literals are written between forward slashes: /pattern/flags. The RegExp constructor allows dynamic patterns: new RegExp(pattern, flags). Key methods include String.prototype.match(), String.prototype.replace(), String.prototype.split(), and RegExp.prototype.test(). Named capture groups use the syntax (?<name>...).

In Python, the re module provides regex support. Patterns are typically written as raw strings (r'pattern') to avoid double-escaping backslashes. Key functions include re.match() (matches only at the start of the string), re.search() (finds the first match anywhere), re.findall() (returns all matches), and re.sub() (substitutes matches). The re.compile() function pre-compiles a pattern for repeated use, improving performance in loops.

In Java, the java.util.regex package provides Pattern and Matcher classes. In PHP, the PCRE (Perl-Compatible Regular Expressions) functions (preg_match, preg_replace, preg_split) are the standard. All these flavours share the core regex syntax but have minor differences in flags, named groups syntax, and lookaround support.

Debugging Regular Expressions

Regex patterns can be difficult to debug because errors are often invisible β€” the pattern simply matches the wrong things or matches nothing. The most common debugging strategies include: breaking complex patterns into smaller parts and testing each individually; using this tool's live match highlighting to immediately see what is being matched; checking for missing or extra escape characters (a backslash before a character in a character class is different from a backslash outside one); verifying that special characters like ., *, +, ?, (, ), [, ], {, }, ^, $, |, and \ are escaped when you want to match them literally.

Frequently Asked Questions

The most common causes are: forgetting to anchor the pattern with ^ and $ (without anchors, the pattern can match anywhere in the string); using greedy quantifiers (*, +) when you need lazy ones (*?, +?); not escaping special characters; and confusing character classes ([abc]) with groups ((abc)). Use the live highlighting in this tool to see exactly what is being matched and refine accordingly.

Greedy quantifiers (*, +, {n,}) match as many characters as possible. Lazy quantifiers (*?, +?, {n,}?) match as few characters as possible. For example, applied to the string <b>bold</b>, the greedy pattern <.*> matches the entire string (including both tags), while the lazy pattern <.*?> matches only <b>. Use lazy quantifiers when you need to match the shortest possible string between two delimiters.

Named capture groups use the syntax (?<name>pattern) and let you reference the captured text by a descriptive name rather than a numeric index. For example, the pattern (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) captures year, month, and day fields by name from an ISO date string, making the code that uses the match result much more readable and maintainable. This tester shows named group matches in the detail section below the highlighted output.

By default, the dot metacharacter matches any character except a newline. This is by design β€” patterns written to match a "line" of text would break if the dot crossed line boundaries. The s (dotall) flag changes this behaviour: with s enabled, the dot matches any character including newlines. Enable the s checkbox in this tool when you need to match patterns that span multiple lines.

Yes. The patterns shown in the common patterns library are standard JavaScript-compatible regex patterns. Copy the pattern from the input field and use it in a JavaScript regex literal or new RegExp() constructor. For Python, remember to use raw strings (r'...') to avoid double-escaping backslashes. The patterns follow standard PCRE/ES2018 syntax which is compatible across most modern languages.

Catastrophic backtracking (also called ReDoS β€” Regular Expression Denial of Service) occurs when a poorly written regex with nested quantifiers takes exponential time to determine that a string does not match. For example, a pattern like (a+)+ applied to a non-matching string of many 'a' characters causes the engine to explore an exponential number of ways to partition the characters before giving up. To avoid it: use atomic groups or possessive quantifiers if your engine supports them; avoid nested quantifiers on similar character classes; and test your patterns against long non-matching inputs as well as matching ones.

Related Free Tools

Need a custom tool built for your business?

Get a Free Quote