Regular Expression Tester
Test your JavaScript regular expressions against any text and clearly highlight all matches and capture groups. Includes a replace mode and a complete regular expression reference.
/
/
g
Back-references:
$1…$9, $<name>, $& (whole match), $` / $' (text before/after), $$ (a literal $).Ctrl + Enter
Load example
Result
Regular Expression — Documentation
This tester uses the JavaScript regular expression engine of your browser (ECMAScript 2018+, the same engine as Node.js): matches are computed locally, and results are updated while you type. The table below lists every construct supported by JavaScript patterns.
| Character | What does it do? |
|---|---|
| Anchors and boundaries | |
^ | Matches at the beginning of the input. With the m flag, also right after a line break. |
$ | Matches at the end of the input. With the m flag, also right before a line break. |
\b | Word boundary: the position between a word character (\w) and a non-word character. \bcat\b matches "cat" but not "concatenate". |
\B | Non-word boundary: the opposite of \b. |
| Characters and classes | |
. | Any single character except line terminators (\n, \r,
,
). With the s flag it also matches line terminators. |
\ | Escapes the next character: \. is a literal dot, \\ a literal backslash, \/ a slash. Before a letter it forms a special sequence (see below). |
[abc] | Character class: any one of the listed characters. [.] is a literal dot (most metacharacters lose their meaning inside a class). |
[^abc] | Negated class: any character that is not listed. |
[a-z] | Range: any character between a and z (by code point). Combine: [a-zA-Z0-9_]. A literal - must be first, last or escaped. |
\d / \D | A digit [0-9] / a non-digit. |
\w / \W | A word character [A-Za-z0-9_] / a non-word character. Accented letters are not word characters (use \p{L} with u). |
\s / \S | Whitespace (space, tab, line breaks, form feed and Unicode spaces) / non-whitespace. |
\n \r \t \v \f \0 | Line feed, carriage return, tab, vertical tab, form feed, NUL character. |
\xhh | The character with hexadecimal code hh: \x41 is "A". |
\uhhhh, \u{hhhhh} | The Unicode character with code hhhh: é is "é". The braces form (code points above FFFF such as \u{1F600}) requires the u flag. |
\cX | The control character Ctrl-X: \cM is a carriage return. |
\p{…} / \P{…} | Unicode property (requires u): \p{L} any letter, \p{Lu} upper-case letter, \p{N} number, \p{P} punctuation, \p{Script=Greek}, \p{Emoji}. \P{…} negates. |
| Quantifiers (apply to the preceding item) | |
* | Zero or more times: bo* matches "b", "bo", "boooo". |
+ | One or more times: bo+ matches "bo", "boooo" but not "b". |
? | Zero or one time (optional): colou?r matches "color" and "colour". |
{n} | Exactly n times: \d{4} matches four digits. |
{n,} | At least n times: \d{2,}. |
{n,m} | Between n and m times: \d{2,4}. |
*? +? ?? {n,m}? | Lazy (non-greedy) versions: match as little as possible. <.+?> matches a single tag instead of everything between the first and the last tag. |
| Groups and alternation | |
(x) | Capturing group: matches x and remembers it. Groups are numbered from 1 by their opening parenthesis and can be reused with $1 in replacements or \1 in the pattern. |
(?:x) | Non-capturing group: groups without remembering. Use it for alternation or quantifiers: (?:ab)+. |
(?<name>x) | Named capturing group. Referenced with \k<name> in the pattern and $<name> in replacements. |
x|y | Alternation: matches x or y. cat|dog. Precedence is lowest, so group when needed: gr(a|e)y. |
\1 … \9, \k<name> | Back-reference: matches the same text as the group captured. (\w)\1 matches a doubled letter, (["'])(.*?)\1 a quoted string. |
| Lookaround (zero-width assertions: they check without consuming) | |
x(?=y) | Positive lookahead: x only if followed by y. \d+(?=€) matches the amount but not the currency symbol. |
x(?!y) | Negative lookahead: x only if not followed by y. \d+(?!\d|px). |
(?<=y)x | Positive lookbehind: x only if preceded by y. (?<=\$)\d+ matches the number after a dollar sign. |
(?<!y)x | Negative lookbehind: x only if not preceded by y. |
| Replacement patterns (the "Replace with" field) | |
$1 … $99 | The text captured by group n. |
$<name> | The text captured by the named group. |
$& | The whole match. |
$` / $' | The text before / after the match. |
$$ | A literal dollar sign. |
| Flags | |
g | Global: find every match instead of stopping after the first one. Also required for "replace all". |
i | Case-insensitive matching. |
m | Multiline: ^ and $ match at the start and end of every line. |
s | Dot-all: . also matches line terminators. |
u | Unicode: patterns are treated as sequences of code points, enables \p{…} and \u{…}, and makes the syntax stricter (unnecessary escapes such as \- outside a class become errors). |
y | Sticky: the match must start exactly at the current position (lastIndex). Mostly useful for tokenizers. |
Regular expression solutions to common problems
Copy any of these patterns into the tester (they are also available as "Load example" chips). Most of them are intentionally pragmatic: for instance no regular expression can fully validate an email address as defined by RFC 5322, but the one below accepts what real users type.
| Problem | Regular expression | Notes |
|---|---|---|
| Email address | ^[\w.+-]+@[\w-]+(\.[\w-]+)+$ | Local part, @, domain with at least one dot. Add the i flag. |
| URL (http/https) | ^https?:\/\/[\w.-]+(?::\d+)?(?:\/[^\s?#]*)?(?:\?[^\s#]*)?(?:#\S*)?$ | Scheme, host, optional port, path, query string and fragment. |
| IPv4 address | ^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$ | Each octet 0–255, no leading zeroes. |
| IPv6 address | ^(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}$|^(?:[0-9a-f]{1,4}:)*::(?:[0-9a-f]{1,4}:)*[0-9a-f]{0,4}$ | Full form or compressed with one ::. Use the i flag. |
| Date YYYY-MM-DD | ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ | Checks the shape and ranges, not the number of days of each month. |
| Time HH:MM (24 h) | ^([01]\d|2[0-3]):[0-5]\d$ | Add (:[0-5]\d)? for optional seconds. |
| ISO 8601 date-time | ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})$ | e.g. 2024-05-17T14:30:00Z or 2024-05-17T14:30+02:00. |
| Hex color | ^#(?:[0-9a-f]{3}){1,2}$ | #abc or #aabbcc, with the i flag. Add |[0-9a-f]{4}|[0-9a-f]{8} variants for alpha. |
| US phone number | ^(?:\+?1[-. ]?)?\(?([2-9]\d{2})\)?[-. ]?(\d{3})[-. ]?(\d{4})$ | Accepts (555) 123-4567, 555.123.4567, +1 555 123 4567; groups capture area code, exchange and number. |
| US ZIP code | ^\d{5}(?:-\d{4})?$ | 12345 or 12345-6789. |
| UK postcode | ^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$ | e.g. SW1A 1AA, with the i flag. |
| Canadian postal code | ^[A-CEGHJ-NPR-TVXY]\d[A-CEGHJ-NPR-TV-Z] ?\d[A-CEGHJ-NPR-TV-Z]\d$ | e.g. K1A 0B1. |
| Strong password | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}$ | At least 8 characters with a lower-case letter, an upper-case letter, a digit and a symbol (lookaheads). |
| HTML tag | <\/?([a-z][a-z0-9-]*)\b[^>]*> | Opening or closing tag; group 1 is the tag name. Use gi. Do not parse full HTML with regex. |
| Trim whitespace | ^\s+|\s+$ | Replace with an empty string (flag g). \s+ replaced by a single space collapses runs of whitespace. |
| Credit card digits | ^(?:\d[ -]?){12,18}\d$ | 13–19 digits with optional spaces or dashes. Validate with the Luhn algorithm afterwards. |
| UUID / GUID | ^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ | RFC 4122/9562 layout, with the i flag. |
| URL slug | ^[a-z0-9]+(?:-[a-z0-9]+)*$ | Lower-case words separated by single dashes. |
| Floating-point number | ^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$ | Integers, decimals and scientific notation: 42, -3.14, .5, 1e-9. |
| Integer with thousands separators | ^-?\d{1,3}(?:,\d{3})*$ | 1,234,567. |
| Duplicate words | \b(\w+)\s+\1\b | Finds "the the". Use the gi flags and replace with $1. |
| Quoted string | (["'])(?:(?!\1)[^\\]|\\.)*\1 | Handles escaped quotes inside the string. |
| Twitter/X handle, hashtag | (?<![\w@])@(\w{1,15})\b, #(\w+) | Lookbehind prevents matching inside email addresses. |
| Semantic version | ^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?$ | major.minor.patch with optional pre-release and build metadata. |