Regex tester
Write a regular expression and see what it matches as you type, with groups and replace.
Matches
Ready to use
Common regex patterns
Pick a pattern and press Try it to load it into the tester with some sample text, then adjust it to your data. The email, URL and phone patterns are pragmatic: they catch typing mistakes and junk, but they don't follow every rule of the standards, and no pattern can tell you whether an address or number actually exists.
| Pattern | Regex | Load into the tester |
|---|---|---|
| Email addressPragmatic: something@something.something, no spaces. | ^[^\s@]+@[^\s@]+\.[^\s@]+$ Flags: gm | |
| URL (http or https)Pragmatic: a host with a dot, then an optional port, path, query and fragment. | ^https?:\/\/[\w-]+(?:\.[\w-]+)+(?::\d+)?(?:[/?#]\S*)?$ Flags: gim | |
| IPv4 addressFour numbers from 0 to 255, without leading zeros. | ^(?:(?: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)$ Flags: gm | |
| Hex colorCSS hex colors: #rgb, #rgba, #rrggbb and #rrggbbaa. | ^#(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$ Flags: gim | |
| Date (YYYY-MM-DD)Checks the format, not the calendar: 2024-02-31 passes. | ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$ Flags: gm | |
| Time (24-hour HH:MM)From 00:00 to 23:59, with a leading zero. | ^(?:[01]\d|2[0-3]):[0-5]\d$ Flags: gm | |
| Digits onlyOne or more digits 0–9, nothing else. | ^\d+$ Flags: gm | |
| Letters onlyLetters in any language, thanks to \p{L} and the u flag. | ^\p{L}+$ Flags: gmu | |
| Phone numberPragmatic: an optional +, then 7 to 15 digits with spaces, dots, dashes or brackets. | ^\+?(?:[ ().-]*\d){7,15}[ ().-]*$ Flags: gm | |
| Password with a digitAt least 8 characters, including at least one digit. | ^(?=.*\d).{8,}$ Flags: gm | |
| Leading and trailing spacesSpaces and tabs at the start or end of each line. Replace with nothing to trim. | ^[ \t]+|[ \t]+$ Flags: gm | |
| Duplicate wordsThe same word twice in a row, like "the the". Replace with $1 to fix. | \b(\w+)\s+\1\b Flags: gi |
Regular expression cheat sheet
| Pattern | Matches |
|---|---|
. | Any character except a line break |
\d \w \s | A digit, a word character (letter, digit, _), whitespace |
\D \W \S | Anything except a digit, word character or whitespace |
\. \* \\ | A literal dot, asterisk or backslash (see escaping below) |
[abc] [^abc] [a-z] | One of a, b, c; anything but those; a range |
* + ? | Zero or more, one or more, zero or one of the previous item |
{3} {3,} {2,5} | Exactly 3; 3 or more; between 2 and 5 |
*? +? | Lazy: as few as possible |
^ $ | Start and end of the text (or line, with the m flag) |
\b | A word boundary |
(…) (?:…) (?<name>…) | Capturing, non-capturing and named groups |
\1 \k<name> | Backreference: the same text that group 1, or the group called name, matched |
a|b | a or b |
(?=…) (?!…) | Lookahead: followed by …, not followed by … |
(?<=…) (?<!…) | Lookbehind: preceded by …, not preceded by … |
\p{L} \P{L} | A letter in any language; anything but a letter (needs the u flag) |
Escaping special characters
These characters have a special meaning: . * + ? ^ $ { } ( ) [ ] | \. To match one of them as itself, put a backslash in front: \. matches a dot, \( a bracket and \\ a backslash. In a JavaScript regex literal such as /a\/b/, escape the slash too. When you build a pattern from a string with new RegExp(), every backslash has to be doubled: new RegExp("\\d+").
Regex flags
Flags change how the whole pattern works. They go after the closing slash, as in /abc/gi, or in the second argument of new RegExp("abc", "gi"). The tester above offers the five you need most.
| Flag | Property | What it does |
|---|---|---|
d | hasIndices | Adds the start and end position of the match and each group (match.indices). |
g | global | Finds every match, not just the first. |
i | ignoreCase | Ignores the difference between A and a. |
m | multiline | ^ and $ match at the start and end of each line, not just of the whole text. |
s | dotAll | . also matches line breaks. |
u | unicode | Reads the pattern as Unicode code points, so . matches a whole emoji and \p{…} works. |
v | unicodeSets | An upgrade of u that adds set operations in classes, like [\p{Lowercase}&&\p{Script=Greek}]. Can’t be combined with u. |
y | sticky | Matches only at exactly lastIndex, without searching further along the text. |
More tools
More on the bench
- JSON formatter and validatorPretty-print, minify and validate JSON, with the exact line of any error.
- Base64 encoder and decoderEncode text to Base64 and back, including URL-safe Base64 and emoji.
- URL encoder and decoderPercent-encode and decode URLs and query values, and break a URL into parts.
- Text diff checkerCompare two texts or code snippets line by line or word by word.
- CSV to JSON and JSON to CSVConvert CSV to JSON and JSON to CSV, with delimiter detection.
- UUID (GUID) generatorGenerate random v4 or time-ordered v7 UUIDs, one or up to 1,000 at a time.
- JWT decoderRead the header and claims of a JSON Web Token, with expiry dates in plain English.
- MD5 and SHA hash generatorMD5, SHA-1, SHA-256, SHA-384 and SHA-512 hashes of any text, with a match check.
- Unix timestamp (epoch) converterEpoch seconds or milliseconds to a readable date and back in any time zone, ISO 8601 too.
- Lorem Ipsum generatorPlaceholder paragraphs, sentences or words, plain or as HTML.
Frequently asked questions
Which regex flavor does this use?
JavaScript’s, exactly as in Chrome, Firefox, Safari and Node.js, because it runs on your browser’s own engine. Most syntax is shared with PCRE (PHP), Python and Java, but some features differ, such as lookbehind support in older engines and possessive quantifiers.
What do the flags mean?
g finds every match instead of only the first. i ignores case. m makes ^ and $ match at the start and end of each line. s lets . match line breaks too. u enables full Unicode matching, including \p{L} for any letter. The flags table on this page also covers d, v and y.
How do I use capture groups in a replacement?
Refer to numbered groups as $1, $2 and so on, and to named groups as $<name>. $& inserts the whole match. Turn on Replace to try it: the example rewrites 2024-03-10 as 10/03/2024.
Why did my pattern stop with a timeout?
Some patterns, typically nested quantifiers like (a+)+ or (\w*)*, can take exponentially long on text that almost matches. This is called catastrophic backtracking. The tester stops after 1.5 seconds; rewrite the pattern so each character can only be matched in one way.
Is my text sent anywhere?
No. Matching runs in your browser, in a background thread, and nothing is uploaded.