Pattern matching
Regex Tester
Test regular expressions with live match highlighting, capture groups, and flag controls.
How Regular Expressions Work
A regular expression (regex) is a sequence of characters that defines a search pattern. They are used in virtually every programming language for validating input, extracting data, and transforming strings. JavaScript, Python, Java, Go, and most Unix tools all support regex with largely compatible syntax.
Common Patterns
\d matches any digit (0–9). \w matches any word character (letters, digits, underscore). \s matches whitespace. . matches any character except newline (use the s flag to include newlines). ^ anchors to the start of a string, $ to the end. \b is a word boundary — useful for matching whole words without capturing surrounding characters.
Quantifiers
* means zero or more, + means one or more, ? means zero or one. {n} matches exactly n times, {n,m} matches between n and m times. Add ? after any quantifier to make it lazy (match as few characters as possible) rather than greedy: .*? versus .*.
Capture Groups
Parentheses create capture groups — (\d{4})-(\d{2})-(\d{2}) matches a date and captures the year, month, and day separately. Named groups use the syntax (?<name>pattern). Non-capturing groups use (?:pattern) when you need grouping for alternation or quantifiers but don't need to capture the value. Use our URL Encoder if you need to include regex patterns in a URL query string.
Flags
The g (global) flag finds all matches rather than stopping at the first. The i flag makes matching case-insensitive. The m flag makes ^ and $ match the start and end of each line rather than the whole string. The s (dotAll) flag makes . match newlines.
Frequently Asked Questions
- What is the difference between greedy and lazy matching?
- Greedy quantifiers (
*,+,{n,m}) match as many characters as possible while still allowing the overall pattern to match. Lazy quantifiers (*?,+?,{n,m}?) match as few characters as possible. For example, given the string<b>bold</b>, the pattern<.*>matches the entire string greedily, while<.*?>matches just<b>. - How do I match a literal dot, bracket, or other special character?
- Escape it with a backslash. The special characters that need escaping are:
. * + ? ^ $ { } [ ] | ( ) \. For example, to match a literal dot use\.— without the backslash,.matches any character. To match a literal opening bracket use\(. - What does the global flag do?
- Without the
gflag, a regex stops after the first match. Withg, it finds all non-overlapping matches in the string. This is essential when usingString.matchAll()orString.replace()to replace all occurrences. Withoutg,replace()only replaces the first match. - How do I match an email address with regex?
- A basic pattern is
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}. This covers the vast majority of real email addresses. A fully RFC 5321-compliant email regex is thousands of characters long and rarely practical. For production validation, use a simple pattern to catch obvious errors and confirm deliverability by sending a verification email. - What is a lookahead and lookbehind?
- Lookaheads and lookbehinds are zero-width assertions — they match a position based on what comes before or after, without consuming characters. Positive lookahead
(?=pattern)matches if followed by pattern. Negative lookahead(?!pattern)matches if not followed by pattern. Lookbehind uses(?<=pattern)and(?<!pattern). Example:\d+(?= dollars)matches a number only when followed by " dollars". - Why does my regex work in one language but not another?
- Regex flavours differ across languages. JavaScript does not support lookbehind in older engines (it was added in ES2018). Python uses
remodule syntax which differs slightly from JavaScript. Go's RE2 engine intentionally excludes backtracking features like lookaheads to guarantee linear-time matching. Always test regex in the language and engine you intend to use it in.