Regex Tester
Test regular expressions with real-time matching and highlighting. Supports all JavaScript regex features.
What Is a Regular Expression?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Originating from formal language theory in the 1950s, regular expressions became a practical programming tool when Ken Thompson built them into the Unix text editor ed in 1968. Today, regex is supported in virtually every programming language, text editor, and command-line tool.
At its simplest, a regex can be a literal string like "hello" that matches exact occurrences of that text. The real power comes from metacharacters and quantifiers that let you describe patterns: \d matches any digit, [a-z] matches any lowercase letter, .+ matches one or more of any character, and ^ and $ anchor the match to the start and end of a line. These building blocks combine to express complex patterns in a compact notation.
Regular expressions are indispensable for tasks like input validation (checking if an email address or phone number matches an expected format), data extraction (pulling dates, URLs, or IDs from unstructured text), and text transformation (find-and-replace operations that would be impossible with simple string matching). While the syntax can be intimidating at first, mastering even basic patterns pays off quickly in daily development work.
How Regular Expressions Work
Under the hood, a regex engine compiles the pattern into an internal representation (typically a finite automaton) and then applies it to the input string. There are two main types of regex engines: DFA (Deterministic Finite Automaton) engines and NFA (Nondeterministic Finite Automaton) engines. JavaScript uses an NFA engine with backtracking.
Backtracking means the engine tries to match the pattern starting from the first character and, if a partial match fails, it backs up and tries alternative paths. This approach enables powerful features like backreferences and lookaheads but can lead to catastrophic backtracking on poorly written patterns. For example, a pattern like (a+)+ applied to a string of many "a"s followed by a non-matching character will cause the engine to explore an exponential number of paths, potentially freezing your application.
This tool uses JavaScript's built-in RegExp constructor, which supports the full ES2018+ feature set including named capture groups (?<name>...), lookbehind assertions (?<=...) and (?<!...), and the dotall (s) flag. Matches are highlighted in real time and detailed information (match text, position, length, and named groups) is shown in the results table.
Common Use Cases
- Form input validation — Validate email addresses, phone numbers, postal codes, dates, passwords, and other structured inputs against expected patterns before processing them.
- Log parsing — Extract timestamps, IP addresses, error codes, and request paths from server logs. Regex is the primary tool for turning unstructured log text into structured data.
- Data extraction and web scraping — Pull specific data points (prices, names, URLs) from HTML, CSV, or plain text. While HTML parsers are preferred for full HTML processing, regex works well for quick extraction from known formats.
- Find and replace in code editors — IDEs like VS Code, IntelliJ, and Sublime Text support regex find-and-replace for refactoring. You can rename variables, restructure imports, or reformat strings across an entire codebase.
- URL routing — Web frameworks use regex patterns (or simplified versions) to match incoming URLs to handler functions. Express.js route parameters, Django URL patterns, and nginx location blocks all rely on regex.
- Data cleaning and transformation — Normalize phone number formats, strip HTML tags, remove extra whitespace, or convert date formats using regex-based replacements.
- Security scanning — Web Application Firewalls (WAFs), intrusion detection systems, and code linters use regex to detect suspicious patterns like SQL injection attempts, XSS payloads, or hardcoded secrets.
- Syntax highlighting — Text editors and code viewers use regex patterns to identify language tokens (keywords, strings, comments, numbers) and apply color coding.
Tips and Best Practices
- Start simple, then refine — Build your pattern incrementally. Start with a broad match and add specificity until it captures exactly what you need. Test against both matching and non-matching examples.
- Use non-greedy quantifiers when appropriate — By default, * and + are greedy (match as much as possible). Adding ? makes them lazy (match as little as possible). For example, <.+?> matches individual HTML tags, while <.+> would match everything from the first < to the last >.
- Anchor your patterns — Use ^ and $ (or \b for word boundaries) to prevent partial matches. Validating an email should match the entire string (^...$ ), not just a substring.
- Beware of catastrophic backtracking — Avoid nested quantifiers like (a+)+ or (.+)* on patterns that could match long strings. If performance is critical, test your regex with long inputs and consider atomic groups or possessive quantifiers (available in some engines).
- Use named capture groups — Instead of referring to groups by number ($1, $2), use named groups (?<year>\d{4}) for self-documenting patterns and more maintainable code.
- Comment complex patterns — In languages that support verbose mode (x flag), add comments to complex patterns. In JavaScript, add a comment above the regex explaining what it matches.
Regex Flavors Compared
JavaScript vs PCRE (PHP, Python re): PCRE (Perl Compatible Regular Expressions) supports features like recursive patterns, possessive quantifiers, and conditional patterns that JavaScript lacks. JavaScript added lookbehind in ES2018 but still cannot do recursive matching.
JavaScript vs .NET: .NET regex supports balancing groups for matching nested structures (like balanced parentheses), which no other major flavor supports. It also allows named groups to capture multiple values.
JavaScript vs RE2 (Go, Rust): RE2 uses a DFA engine that guarantees linear-time matching, eliminating catastrophic backtracking. The trade-off is no support for backreferences or lookaheads. Use RE2-based engines when performance and safety are more important than advanced features.