Regex Tester
Test regular expressions with highlighting
Online Regular Expression Tester - Regex Tester
What are regular expressions (regex) and what are they for?
Regular expressions, known as regex or regexp, are text patterns that describe sets of character strings. They are an extremely powerful tool for searching text that matches complex patterns, validating input formats such as emails, phone numbers, URLs and postal codes, extracting specific information from large bodies of text, running find and replace across editors and code, parsing logs and configuration files, and processing text in any programming language. The syntax looks cryptic at first, but mastering regex is a fundamental skill for any developer.
What is the core regular expression syntax?
The basic metacharacters are . (any character except a newline), * (zero or more of the previous token), + (one or more), ? (zero or one), ^ (start of line), $ (end of line), [] (a character class matching any character inside), () (capture groups), | (alternation, an OR), {} (exact quantifiers such as {3} or ranges such as {1,5}), and \ (escaping a metacharacter). Predefined classes shorten common patterns: \d for digits 0-9, \w for word characters (letters, digits, underscore), \s for whitespace such as spaces, tabs and newlines, and their uppercase negations \D, \W and \S.
How do you use regular expressions in JavaScript?
JavaScript supports regex natively through the literal syntax /pattern/flags or the constructor new RegExp("pattern", "flags"). The main methods are regex.test(string), which returns true or false for validation; string.match(regex), which returns an array of matches or null; string.replace(regex, replacement), which substitutes matches; and string.split(regex), which splits by the pattern. Flags change the behaviour: g for global (all matches), i for case insensitive, m for multiline so ^ and $ apply per line, s for dotall so . also matches newlines, and u for unicode. This tool lets you test patterns interactively before wiring them into your code.
If you work with this tool, you will probably also find these useful Text Escaper, Gradient Generator, Flexbox Generator, Text Diff Checker.
Frequently asked questions
Why is my regular expression slow or freezing the browser?
You are most likely hitting catastrophic backtracking, which happens with ambiguous patterns that can match the same input in many ways. Classic offenders are (a+)+ against an input like "aaaaaaaaaaaaaaaaaaaaab", (.*)*, and any nested quantifier. The regex engine tries exponentially many combinations before it gives up. The fixes are to avoid nested quantifiers such as (a*)*, use possessive or atomic quantifiers where your engine supports them, simplify alternations, and ask whether regex is the right tool at all, since complex parsing often deserves a real parser.
How do I capture groups with readable names in regex?
Use named groups with the (?<name>pattern) syntax, available in JavaScript from ES2018, Python 3.x, PHP 7 and other modern languages. For example, /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/ parses a date, and in JavaScript you read it back with match.groups.year. Named groups are far more readable than numeric indexes such as match[1] and match[2], especially in complex patterns with many captures, and they make the surrounding code easier to maintain.
What is the difference between .* (greedy) and .*? (lazy)?
.* is greedy: it tries to capture as many characters as it can while still allowing the rest of the pattern to match. .*? is lazy or reluctant: it captures as few characters as possible. In the string "abc123def456" the greedy pattern /.+\d/ captures "abc123def456", running all the way to the last digit, while the lazy /.+?\d/ captures only "abc1" and stops at the first digit. Which one you want depends entirely on the behaviour your use case needs.