Regex Tester & Cheat Sheet
Regex (regular expressions) match patterns in text. Common syntax: \d = digit, \w = word character, . = any character, * = 0+ times, + = 1+ times, ^ = start, $ = end. Email pattern: ^[\w.-]+@[\w.-]+\.\w{2,}$
Test and learn regular expressions with live matching, comprehensive cheat sheet, and common patterns library. See all matches highlighted with groups, use search and replace, and explore worked examples.
Regular Expression
Test String
What Are Regular Expressions?
Regular expressions (regex or regexp) are powerful patterns used for matching, searching, and manipulating text. Think of them as a specialized mini-language for describing text patterns. Every major programming language supports regex, making it an essential skill for developers, data analysts, and anyone working with text data.
At their core, regex patterns describe “what to look for” rather than “where to look.” For example, the pattern \d3-\d4 means “find three digits, followed by a hyphen, followed by four digits”—a format that matches phone numbers like 555-1234.
When to Use Regex vs. String Methods
While regex is powerful, it is not always the best tool. For simple tasks like checking if a string contains a substring or splitting on a single character, built-in string methods (includes(), split(), indexOf()) are clearer and often faster.
Use regex when you need to:
- Match patterns with variations (emails, phone numbers, dates)
- Validate complex input formats
- Extract multiple pieces of data from text
- Find and replace with pattern-based transformations
- Work with optional or repeating elements
Prefer string methods when:
- Searching for an exact, literal string
- The operation is simple (starts with, ends with, contains)
- Performance is critical and patterns are simple
Regex Across Programming Languages
Regex syntax is remarkably consistent across languages, though there are subtle differences. This tester uses JavaScript's regex engine, which follows the ECMAScript standard.
- JavaScript:
/pattern/flagsornew RegExp('pattern', 'flags') - Python:
re.search(r'pattern', text)— note therprefix for raw strings - Java:
Pattern.compile("pattern")— double backslashes needed (\\d) - PHP:
preg_match('/pattern/', $text)— delimiters required - Go:
regexp.MustCompile(`pattern`)— uses RE2 engine (no lookaheads)
Most core syntax (\d, \w, *, +, ?, character classes) works identically everywhere. Advanced features like lookbehinds may have varying support.
Tips for Writing Better Regex
- Start simple, then refine. Begin with a basic pattern that matches your target, then add constraints to eliminate false positives.
- Use non-capturing groups
(?:...)when you do not need to extract the matched content—they are slightly more efficient. - Be specific. Instead of
.*(any characters), use[^/]+(any characters except slash) when appropriate. - Anchor your patterns with
^and$when validating entire strings to prevent partial matches. - Comment complex patterns. In many languages, you can use verbose mode to add whitespace and comments.
- Test with edge cases. Empty strings, special characters, and unexpected input often reveal regex bugs.
How to Use
- Enter your value in the input field
- Click the Calculate/Convert button
- Copy the result to your clipboard
Frequently Asked Questions
- What is regex (regular expression)?
- A regular expression (regex or regexp) is a sequence of characters that defines a search pattern for text. Regex can match, search, extract, or replace text based on patterns rather than literal strings. For example, \d{3}-\d{4} matches phone formats like 555-1234, where \d means "any digit" and {3} means "exactly three times." Regex is supported in virtually every programming language including JavaScript, Python, Java, PHP, Go, and is built into tools like grep, sed, and text editors.
- What does the g flag do in regex?
- The "g" flag stands for "global" and tells the regex engine to find ALL matches in the text, not just the first one. Without the g flag, regex stops after finding the first match. For example, /cat/g on "cat cat cat" finds three matches, while /cat/ (no g flag) finds only the first "cat." In JavaScript, the g flag also affects methods like replace() — with g, it replaces all occurrences; without it, only the first.
- How do I match literal special characters in regex?
- To match special regex characters literally, escape them with a backslash (\). Special characters that need escaping include: . * + ? ^ $ { } [ ] \ | ( ). For example: \. matches a literal period, \? matches a question mark, \( matches an opening parenthesis. Inside character classes [brackets], most special characters lose their meaning except for ^ - \ ], so [.+*] matches a literal period, plus, or asterisk without escaping.
- What is the difference between regex and glob patterns?
- Glob patterns are simpler file-matching patterns used in shells and file systems, while regex is a full pattern-matching language for text. Key differences: In glob, * means "any characters" but in regex * means "zero or more of the previous character." Glob uses ? for single character, regex uses . for any character. Glob patterns like *.txt match filenames; regex like \.txt$ matches text content. Regex is far more powerful, supporting groups, lookaheads, quantifiers, and alternatives that glob cannot express.
- What are capture groups and how do I use them?
- Capture groups are created with parentheses () and let you extract specific portions of a match. In the pattern (\d{3})-(\d{4}) matching "555-1234", group 1 captures "555" and group 2 captures "1234." In replacement strings, reference groups with $1, $2 (JavaScript, PHP) or \1, \2 (Python, sed). Use (?:...) for non-capturing groups when you need grouping but do not need to extract the value. Named groups (?<name>...) make patterns more readable.
- When should I use regex vs simple string methods?
- Use simple string methods (indexOf, includes, startsWith, split) when: checking for exact substrings, the pattern is a literal string, or performance is critical for simple operations. Use regex when: you need pattern matching with variations (emails, phone numbers), validating complex formats, extracting multiple pieces of data, or doing pattern-based find-and-replace. Regex adds overhead, so for "does string contain 'hello'?", use includes("hello") rather than /hello/.test(string).
- What are lookaheads and lookbehinds in regex?
- Lookaheads and lookbehinds are "zero-width assertions" that check for patterns without including them in the match. Positive lookahead (?=...) matches if the pattern follows, negative lookahead (?!...) matches if the pattern does NOT follow. For example, \d+(?= dollars) matches numbers followed by " dollars" but does not include " dollars" in the match. Lookbehinds (?<=...) and (?<!...) check what comes before. Note: lookbehinds have limited support in some regex engines (JavaScript added them in ES2018, Go does not support them).
- How do I validate an email address with regex?
- A basic email regex is: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ — This matches: one or more valid local-part characters, an @ symbol, a domain name, a dot, and a TLD of 2+ letters. However, the official email RFC is extremely complex, and perfect validation is nearly impossible with regex alone. For production use, a simple regex to catch obvious typos is fine, but always verify emails by actually sending to them. Do not reject valid but unusual addresses like user+tag@sub.domain.co.uk.