| Pattern | Matches |
|---|---|
. | Any single character except newline (use the s flag if you also want newlines). |
\d | A digit. By default in ICU/NSRegularExpression this is 0-9 only; turn on Unicode for full digits. |
\w | A "word" character: letters, digits, underscore. |
\s | Any whitespace: space, tab, newline. |
[abc] | One of a, b, or c. |
[^abc] | Anything that is not a, b, or c. |
[a-z] | Any lowercase ASCII letter. Use [a-zA-Z] for both cases or just add the i flag. |
| Pattern | Meaning |
|---|---|
? | 0 or 1 (optional). |
* | 0 or more. |
+ | 1 or more. |
{3} | Exactly 3. |
{2,5} | Between 2 and 5. |
{2,} | 2 or more. |
+?, *? | Lazy versions. Match as little as possible instead of as much as possible. |
Lazy versus greedy is the source of more wrong matches than anything else. If you write ".*" against the string "foo" and "bar", you get the whole thing because * is greedy. ".*?" gives you just "foo".
| Pattern | Meaning |
|---|---|
^ | Start of the string (or line, with the m flag). |
$ | End of the string (or line, with m). |
\b | Word boundary. Useful for whole-word matches: \bcat\b matches "cat" but not "category". |
\B | Anywhere that is not a word boundary. |
Parentheses do two things at once: they group, and they capture.
| Pattern | Meaning |
|---|---|
(abc) | Group and capture, accessible as $1 or \1. |
(?:abc) | Group only, no capture. Use this when you just need | or quantifiers and do not want extra capture slots. |
(?<name>abc) | Named capture. Reference with \k<name> or ${name}. |
a|b | Alternation: a or b. |
These check for context without consuming characters. Read once, you will never forget them.
| Pattern | Meaning |
|---|---|
(?=foo) | Followed by "foo". a(?=b) matches the "a" in "ab" but not in "ac". |
(?!foo) | Not followed by "foo". |
(?<=foo) | Preceded by "foo". |
(?<!foo) | Not preceded by "foo". |
| Flag | Effect |
|---|---|
i | Case insensitive. |
m | Multiline: ^ and $ match start/end of each line. |
s | Dotall: . also matches newlines. |
u | Unicode: full Unicode awareness for \d, \w, etc. |
x | Extended: ignore whitespace and allow comments inside the pattern. |
Email-ish (catches almost anything that looks like an email, not RFC-strict):
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URL:
https?://[^\s]+
UK / US phone, very loose:
\+?\d[\d\s().-]{7,}\d
ISO date YYYY-MM-DD:
\b\d{4}-\d{2}-\d{2}\b
IPv4 (loose):
\b(?:\d{1,3}\.){3}\d{1,3}\b
Hex color:
#?[0-9a-fA-F]{6}\b
Trim multiple blank lines into one (replace mode, replacement = \n\n):
(\r?\n){3,}
Most regex flavours treat these as special characters and require a backslash if you want the literal version: . ^ $ * + ? ( ) [ ] { } | \. Inside a character class ([ ]), most of these are already literal, but ] \ and - (in the middle of a class) still need attention.
When you copy a regex into a JSON string or a JavaScript source file, every backslash has to be doubled. \d in your pattern becomes "\\d" in JSON. This is the second-most common reason a working regex stops working when pasted somewhere.
Build, test, and debug regular expressions on iPhone, iPad, and Mac with live match highlighting. · iPhone, iPad & Mac