The regex syntax you will actually reach for, plus copy-ready patterns for email, URLs, dates, and phone numbers. Print it, and test every pattern before you trust it with real data.
Regular expressions (regex) describe a pattern of text, and a regex engine finds every match. The same core syntax works in JavaScript, Python, and most text editors, but a few features differ by flavor, so notes below flag the exceptions. Test every pattern before you use it in real data.
| Pattern | Matches |
|---|---|
. | Any single character except a line break |
[abc] | Any one of a, b, or c |
[^abc] | Any one character except a, b, or c |
[a-z] | Any one lowercase letter; [A-Z] is uppercase, [0-9] is a digit |
\d | Any digit, same as [0-9] |
\D | Any character that is not a digit |
\w | A word character: a letter, digit, or underscore |
\W | Any character that is not a word character |
\s | Any whitespace: space, tab, or line break |
\S | Any character that is not whitespace |
\b | A word boundary, the edge between a word character and a non-word character |
| Pattern | Matches |
|---|---|
* | Zero or more of the previous item (greedy) |
+ | One or more of the previous item (greedy) |
? | Zero or one of the previous item |
{n} | Exactly n of the previous item |
{n,} | n or more of the previous item |
{n,m} | Between n and m of the previous item |
*? +? ?? | The lazy versions: match as little as possible instead of as much as possible |
Greedy is the default: .+ on <b>one</b> and <b>two</b> matches the whole line, while .+? stops at the first closing tag.
| Pattern | Matches |
|---|---|
^ | The start of the string (or the start of a line with the multiline flag) |
$ | The end of the string (or the end of a line with the multiline flag) |
\A | The start of the string, always (not supported in JavaScript) |
\z | The end of the string, always (not supported in JavaScript) |
\bword\b | The whole word word, not part of a longer word |
| Pattern | Matches |
|---|---|
(abc) | A capturing group: matches abc and remembers it for later use |
(?:abc) | A non-capturing group: groups without remembering |
a|b | Either a or b |
(?<year>\d{4}) | A named group called year (JavaScript, Python, and .NET; not in older flavors) |
(?=abc) | A lookahead: matches only if abc follows, without consuming it |
(?!abc) | A negative lookahead: matches only if abc does not follow |
(?<=abc) | A lookbehind: matches only if abc comes before (not in older JavaScript) |
| Pattern | Matches |
|---|---|
\. \* \+ \? \( \) \[ \] \{ \} \^ \$ \| | The literal character, not its special meaning |
\\ | A literal backslash |
\n | A line break |
\t | A tab |
These are starting points for everyday validation, not perfect checkers. Each one is written to match the whole string, so keep the ^ and $ anchors.
| What it checks | Pattern | Notes |
|---|---|---|
| Email address | ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ | Covers everyday addresses. Real email rules allow rare edge cases this rejects, so for strict checking, send a confirmation email instead. |
| Web URL | ^https?://[^\s]+$ | Checks the shape only. It accepts any address starting with http:// or https:// with no spaces. |
| Date, YYYY-MM-DD | ^\d{4}-\d{2}-\d{2}$ | Checks the shape only. It would accept 2026-13-45, which is not a real date, so validate the numbers separately. |
| US phone number | ^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ | Matches (555) 123-4567, 555-123-4567, 555.123.4567, and 5551234567. |
| IPv4 address | ^(?:\d{1,3}\.){3}\d{1,3}$ | Checks the shape only. It accepts 999.999.999.999, so check each number is 0 to 255 separately. |
| Hex color | ^#(?:[0-9a-fA-F]{3}){1,2}$ | Matches #fff and #ffffff, in upper or lower case. |
| Username, 3 to 16 characters | ^[a-zA-Z0-9_]{3,16}$ | Letters, digits, and underscores only. |
| Collapse repeated spaces | \s+ replaced with a single space | Useful for cleaning pasted text. |
\A and \z are not available everywhere. When a pattern works in one tool and fails in another, the flavor is the first thing to check.Why does my regex match too much text? Quantifiers are greedy by default: .+ takes as much as it can. Add a question mark to make it lazy, so .+? stops at the first possible end, or replace the dot with a more specific class, such as [^<]+ to stop at a closing tag.
Do I need to escape a dot in a pattern? Yes, when you mean a literal dot. A bare . matches any character, so example.com as a pattern would also match exampleXcom. Write example\.com to match the dot itself.
Is the email pattern on this page perfect? No. The official email rules allow rare addresses that the pattern rejects, and it accepts some that no mail server would deliver to. It covers the addresses people actually type, and for anything stricter, the reliable check is sending a confirmation email.