Regex Tester
Test regular expressions against text and see matches in real-time.
Regex Quick Reference:
\d- digit,\w- word char,\s- whitespace+- one or more,*- zero or more,?- optional[abc]- character class,(group)- capture group^- start,$- end,.- any char
Metacharacters and Character Classes
Regular expressions use metacharacters — symbols with special meaning. The dot . matches any single character except a newline. ^ anchors the match to the start of a line, $ to the end. Shorthand character classes cover common sets: \d matches any digit (0–9), \w matches word characters (letters, digits, underscore), and \s matches whitespace (space, tab, newline). Their uppercase counterparts — \D, \W, \S — match the opposite. Custom character classes use square brackets: [aeiou] matches any vowel, [^aeiou] matches any non-vowel, and [a-z0-9] matches any lowercase letter or digit.
Quantifiers: Greedy vs Lazy
Quantifiers control how many times a pattern must match. * 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. By default quantifiers are greedy — they consume as many characters as possible. Appending ? makes them lazy: .*? matches as few characters as possible. For HTML like <b>bold</b>, the pattern <.*> greedily matches the entire string, while <.*?> matches only <b>.
Word boundary \b matches the position between a word character and a non-word character — useful for matching whole words without partial matches.
Groups and Lookaheads
Parentheses create capturing groups: (\d{4}-\d{2}-\d{2}) captures an ISO date and lets you reference the matched text. Non-capturing groups use (?:...) when you need grouping for alternation or quantifiers but don't need the captured text. Lookahead assertions match a position without consuming characters: (?=...) is a positive lookahead (the pattern must follow), (?!...) is a negative lookahead (the pattern must not follow). For example, \w+(?=@) matches the username part of an email address without including the @ sign in the match.
JavaScript Regex vs PCRE
JavaScript uses a flavour close to but not identical to PCRE (Perl Compatible Regular Expressions). JavaScript ES2018 added lookbehind assertions ((?<=...) and (?<!...)) and named capture groups ((?<name>...)). PCRE supports possessive quantifiers and atomic groups for controlling backtracking, which JavaScript does not. Catastrophic backtracking — where a poorly written pattern causes exponential execution time on certain inputs — is a real concern in JavaScript. Patterns like (a+)+ applied to a long non-matching string can lock a browser tab. Prefer specific, anchored patterns and test with adversarial inputs.
Worked Examples
Example 1 — Extracting email addresses. Pattern [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} with flag g against the string "Contact [email protected] or [email protected]" returns two matches. Note this is a deliberately simple pattern — true RFC 5322 email validation requires hundreds of characters.
Example 2 — Finding phone numbers (US). Pattern \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} matches (555) 123-4567, 555.123.4567, 555 123 4567, and 5551234567. The \(? and \)? make the parentheses optional; [-.\s]? accepts one optional separator.
Example 3 — Validating an ISO date. Pattern ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ with flag anchors ^...$ accepts 2026-04-19 but rejects 2026-13-01 (invalid month) and 2026-02-30 (structurally valid but still a nonsense date — regex cannot enforce calendar rules).
Example 4 — Redacting credit card numbers in a log. Pattern \b(?:\d[ -]*?){13,16}\b with flag g catches 13–16 digit sequences possibly separated by spaces or hyphens. Pair with JavaScript .replace(pattern, '****') to mask them before writing to disk.
Common Pitfalls
- Forgetting to escape the dot.
example.com(unescaped) matches exampleXcom. Useexample\.comto match a literal dot. - Greedy matches swallowing too much.
<.+>on "<a><b>" matches the whole string. Use<.+?>or<[^>]+>. - Catastrophic backtracking. Patterns like
(a+)+$on a long non-matching input can freeze the browser for seconds. Avoid nested quantifiers on overlapping character classes. - Using regex to parse HTML/XML. These languages are not regular — use a DOM parser. Regex only works for simple, well-controlled extraction.
- Flag confusion. Without the
gflag,String.matchAll()throws, and.replace()only replaces the first match. Withg,RegExp.exec()keeps state inlastIndex— call it in a loop.
Frequently Asked Questions
Can I use this regex in Python or Java? Mostly yes — the core syntax is shared. JavaScript-specific features (named groups with ?<name>, sticky flag y) have equivalents in other flavours with slight syntax differences. PCRE and .NET support atomic groups and possessive quantifiers; JavaScript does not.
Why does my regex work online but fail in my code? Most often: the online tool handled the string delimiter for you. In code, you need to double-escape backslashes ("\\d" in a JSON string, "\\\\d" in a Java string literal), or use a raw string (r"\d" in Python).
Should I use regex to validate emails? Only for shape-checking — full RFC 5322 compliance is impractical. The pragmatic approach: require an @, at least one dot after it, and let a confirmation email do the real validation.
What's the difference between ^/$ and \A/\z? JavaScript only has ^ and $, which match start/end of line with the m flag or start/end of string without. PCRE adds \A (absolute start) and \z (absolute end) that ignore multiline mode.
How do I match Unicode letters, not just ASCII? Use the u flag plus \p{Letter} or \p{L} in modern JavaScript. Without u, \w only matches [A-Za-z0-9_] — no accented characters, no CJK.
Related Calculators
Working with structured data? The JSON Formatter & Validator parses API responses and the JWT Decoder inspects tokens. For text transport encodings, try the URL Encoder/Decoder and Base64 Encoder/Decoder. Browse the full Developer Tools category for more.
Disclaimer
This calculator is provided for educational and informational purposes only. While we strive for accuracy, users should verify all calculations independently. We are not responsible for any errors, omissions, or damages arising from the use of this calculator.
Also in Technical
- → ACL Wildcard Tester — Test whether IPs match a Cisco ACL wildcard mask. Step-by-step binary breakdown and multi-IP match table.
- → Bandwidth Calculator — Calculate data transfer time, throughput, and bandwidth requirements
- → Base64 Encoder/Decoder — Encode and decode Base64 strings
- → Color Code Converter — Convert between HEX, RGB, and HSL color formats