Programming Reference
Regex Syntax Reference
A regular expression is a pattern that describes a set of strings: literal characters match themselves, classes like \d match a kind of character, quantifiers like + say how many, and anchors like ^ and $ pin the match to a position. This reference lists every token with a pattern showing it at work.
Browse by Category
Character Classes
13Matching a kind of character rather than a literal one.
Anchors & Boundaries
6Pinning a match to a position rather than a character.
Quantifiers
11How many times the previous token may repeat.
Groups & Alternation
7Capturing, grouping and choosing between branches.
Lookaround
5Asserting what follows or precedes without consuming it.
Escapes & Unicode
9Literal specials, control characters and code points.
Flags & Modifiers
9Options that change how the whole pattern behaves.
Common Patterns
12Ready-made patterns for everyday validation.
All Regex Syntax (72)
Character Classes (13)
Matching a kind of character rather than a literal one.
| Token | Matches | Description | Example |
|---|---|---|---|
. | Any character | Matches any single character except a line break, unless the s (dotAll) flag is set. | a.c → abc, axc |
[abc] | One of these | A character set: matches exactly one of the characters listed inside the brackets. | [aeiou] → any vowel |
[^abc] | None of these | A negated set. The caret only negates when it is the first character inside the brackets. | [^0-9] → any non-digit |
[a-z] | Character range | Matches any character whose code point falls in the range. Ranges can be combined in one set. | [a-zA-Z0-9_] |
\d | Digit | Any digit 0–9. Equivalent to [0-9] in JavaScript without the u flag. | \d{3} → 123 |
\D | Non-digit | Any character that is not a digit — the complement of \d. | \D+ → abc |
\w | Word character | A letter, digit or underscore: [A-Za-z0-9_]. Notably it does not include the hyphen. | \w+ → user_1 |
\W | Non-word character | Anything \w does not match, including spaces and punctuation. | \W → " " or "!" |
\s | Whitespace | Space, tab, line feed, carriage return, form feed and vertical tab. | \s+ → " " |
\S | Non-whitespace | Any character that is not whitespace. Useful for grabbing a token of unknown shape. | \S+ → one word |
[\s\S] | Truly any character | Matches anything including newlines, which plain . does not. The portable alternative to the s flag. | [\s\S]* |
\p{L} | Unicode property | Matches by Unicode property — L is any letter in any script. Requires the u or v flag. | /\p{L}+/u → héllo |
\P{L} | Negated property | The complement of \p — any character without the named property. Also requires the u flag. | /\P{L}/u |
Matches
Any character
Description
Matches any single character except a line break, unless the s (dotAll) flag is set.
Example
a.c → abc, axc
Matches
One of these
Description
A character set: matches exactly one of the characters listed inside the brackets.
Example
[aeiou] → any vowel
Matches
None of these
Description
A negated set. The caret only negates when it is the first character inside the brackets.
Example
[^0-9] → any non-digit
Matches
Character range
Description
Matches any character whose code point falls in the range. Ranges can be combined in one set.
Example
[a-zA-Z0-9_]
Matches
Digit
Description
Any digit 0–9. Equivalent to [0-9] in JavaScript without the u flag.
Example
\d{3} → 123
Matches
Non-digit
Description
Any character that is not a digit — the complement of \d.
Example
\D+ → abc
Matches
Word character
Description
A letter, digit or underscore: [A-Za-z0-9_]. Notably it does not include the hyphen.
Example
\w+ → user_1
Matches
Non-word character
Description
Anything \w does not match, including spaces and punctuation.
Example
\W → " " or "!"
Matches
Whitespace
Description
Space, tab, line feed, carriage return, form feed and vertical tab.
Example
\s+ → " "
Matches
Non-whitespace
Description
Any character that is not whitespace. Useful for grabbing a token of unknown shape.
Example
\S+ → one word
Matches
Truly any character
Description
Matches anything including newlines, which plain . does not. The portable alternative to the s flag.
Example
[\s\S]*
Matches
Unicode property
Description
Matches by Unicode property — L is any letter in any script. Requires the u or v flag.
Example
/\p{L}+/u → héllo
Matches
Negated property
Description
The complement of \p — any character without the named property. Also requires the u flag.
Example
/\P{L}/u
Anchors & Boundaries (6)
Pinning a match to a position rather than a character.
| Token | Matches | Description | Example |
|---|---|---|---|
^ | Start of string | Asserts the position at the start of the input, or of each line when the m flag is set. | ^Hello |
$ | End of string | Asserts the position at the end of the input, or of each line under the m flag. | world$ |
\b | Word boundary | The position between a word character and a non-word character. Matches a whole word rather than a substring. | \bcat\b → "cat", not "category" |
\B | Non-boundary | Any position that is not a word boundary — matches inside a word. | \Bcat\B → "concatenate" |
\A | Absolute start | Start of the input regardless of the multiline flag. Available in PCRE and Python; JavaScript has no equivalent. | \Astart |
\z | Absolute end | End of the input, ignoring multiline. PCRE and Python only; \Z allows a trailing newline. | end\z |
Matches
Start of string
Description
Asserts the position at the start of the input, or of each line when the m flag is set.
Example
^Hello
Matches
End of string
Description
Asserts the position at the end of the input, or of each line under the m flag.
Example
world$
Matches
Word boundary
Description
The position between a word character and a non-word character. Matches a whole word rather than a substring.
Example
\bcat\b → "cat", not "category"
Matches
Non-boundary
Description
Any position that is not a word boundary — matches inside a word.
Example
\Bcat\B → "concatenate"
Matches
Absolute start
Description
Start of the input regardless of the multiline flag. Available in PCRE and Python; JavaScript has no equivalent.
Example
\Astart
Matches
Absolute end
Description
End of the input, ignoring multiline. PCRE and Python only; \Z allows a trailing newline.
Example
end\z
Quantifiers (11)
How many times the previous token may repeat.
| Token | Matches | Description | Example |
|---|---|---|---|
* | Zero or more | Repeats the previous token any number of times, including none. Greedy by default. | ab*c → ac, abc, abbc |
+ | One or more | Repeats the previous token at least once. | ab+c → abc, abbc |
? | Zero or one | Makes the previous token optional. | colou?r → color, colour |
{n} | Exactly n | Repeats the previous token exactly n times. | \d{4} → 2026 |
{n,} | n or more | Repeats at least n times with no upper bound. | \d{3,} → 123, 12345 |
{n,m} | Between n and m | Repeats between n and m times inclusive. No space after the comma — it stops being a quantifier. | \d{2,4} → 12, 1234 |
*? | Lazy zero or more | Takes as little as possible, expanding only when the rest of the pattern fails. | <.*?> → "<b>" not "<b>x</b>" |
+? | Lazy one or more | Same as + but stops at the first position that lets the rest of the pattern match. | ".+?" |
?? | Lazy optional | Prefers not to match the optional token unless required. | a??b |
{n,m}? | Lazy range | Repeats as few times as the range allows. | \d{2,4}? |
*+ | Possessive | Takes as much as possible and never gives any back, preventing catastrophic backtracking. PCRE and Java; not in JavaScript. | \d*+ |
Matches
Zero or more
Description
Repeats the previous token any number of times, including none. Greedy by default.
Example
ab*c → ac, abc, abbc
Matches
One or more
Description
Repeats the previous token at least once.
Example
ab+c → abc, abbc
Matches
Zero or one
Description
Makes the previous token optional.
Example
colou?r → color, colour
Matches
Exactly n
Description
Repeats the previous token exactly n times.
Example
\d{4} → 2026
Matches
n or more
Description
Repeats at least n times with no upper bound.
Example
\d{3,} → 123, 12345
Matches
Between n and m
Description
Repeats between n and m times inclusive. No space after the comma — it stops being a quantifier.
Example
\d{2,4} → 12, 1234
Matches
Lazy zero or more
Description
Takes as little as possible, expanding only when the rest of the pattern fails.
Example
<.*?> → "<b>" not "<b>x</b>"
Matches
Lazy one or more
Description
Same as + but stops at the first position that lets the rest of the pattern match.
Example
".+?"
Matches
Lazy optional
Description
Prefers not to match the optional token unless required.
Example
a??b
Matches
Lazy range
Description
Repeats as few times as the range allows.
Example
\d{2,4}?
Matches
Possessive
Description
Takes as much as possible and never gives any back, preventing catastrophic backtracking. PCRE and Java; not in JavaScript.
Example
\d*+
Groups & Alternation (7)
Capturing, grouping and choosing between branches.
| Token | Matches | Description | Example |
|---|---|---|---|
(…) | Capturing group | Groups tokens and captures what they matched, numbered from 1 in order of opening bracket. | (\d{4})-(\d{2}) |
(?:…) | Non-capturing group | Groups without capturing. Use it whenever you only need the grouping — it keeps numbering clean and is marginally faster. | (?:ab)+ |
(?<name>…) | Named group | Captures under a name, so results are read as match.groups.name instead of by index. | (?<year>\d{4}) |
| | Alternation | Matches the branch on the left or the right. Has the lowest precedence, so it usually needs bracketing. | (cat|dog)s? |
\1 | Backreference | Matches the same text a previous group captured — how you find a repeated word or a matching quote. | (\w+) \1 → "the the" |
\k<name> | Named backreference | Backreference by group name rather than number. | (?<q>["']).*?\k<q> |
$1 | Replacement reference | In a replacement string, inserts what group 1 captured. $<name> works for named groups, $& for the whole match. | replace(/(\w+) (\w+)/, '$2 $1') |
Matches
Capturing group
Description
Groups tokens and captures what they matched, numbered from 1 in order of opening bracket.
Example
(\d{4})-(\d{2})
Matches
Non-capturing group
Description
Groups without capturing. Use it whenever you only need the grouping — it keeps numbering clean and is marginally faster.
Example
(?:ab)+
Matches
Named group
Description
Captures under a name, so results are read as match.groups.name instead of by index.
Example
(?<year>\d{4})
Matches
Alternation
Description
Matches the branch on the left or the right. Has the lowest precedence, so it usually needs bracketing.
Example
(cat|dog)s?
Matches
Backreference
Description
Matches the same text a previous group captured — how you find a repeated word or a matching quote.
Example
(\w+) \1 → "the the"
Matches
Named backreference
Description
Backreference by group name rather than number.
Example
(?<q>["']).*?\k<q>
Matches
Replacement reference
Description
In a replacement string, inserts what group 1 captured. $<name> works for named groups, $& for the whole match.
Example
replace(/(\w+) (\w+)/, '$2 $1')
Lookaround (5)
Asserting what follows or precedes without consuming it.
| Token | Matches | Description | Example |
|---|---|---|---|
(?=…) | Positive lookahead | Asserts what follows without consuming it, so the matched text excludes the assertion. | \d+(?= ?kg) → "80" in "80kg" |
(?!…) | Negative lookahead | Asserts that what follows does not match. The usual way to exclude one case from a broad pattern. | \d+(?!px) |
(?<=…) | Positive lookbehind | Asserts what precedes the position. Supported in modern JavaScript, PCRE and Python. | (?<=£)\d+ → "50" in "£50" |
(?<!…) | Negative lookbehind | Asserts that what precedes does not match. | (?<!£)\d+ |
(?=.*x) | Stacked lookahead | Several lookaheads at one position test independent conditions — the standard password-rule idiom. | ^(?=.*\d)(?=.*[a-z]).{8,}$ |
Matches
Positive lookahead
Description
Asserts what follows without consuming it, so the matched text excludes the assertion.
Example
\d+(?= ?kg) → "80" in "80kg"
Matches
Negative lookahead
Description
Asserts that what follows does not match. The usual way to exclude one case from a broad pattern.
Example
\d+(?!px)
Matches
Positive lookbehind
Description
Asserts what precedes the position. Supported in modern JavaScript, PCRE and Python.
Example
(?<=£)\d+ → "50" in "£50"
Matches
Negative lookbehind
Description
Asserts that what precedes does not match.
Example
(?<!£)\d+
Matches
Stacked lookahead
Description
Several lookaheads at one position test independent conditions — the standard password-rule idiom.
Example
^(?=.*\d)(?=.*[a-z]).{8,}$
Escapes & Unicode (9)
Literal specials, control characters and code points.
| Token | Matches | Description | Example |
|---|---|---|---|
\. | Literal dot | Escapes a metacharacter so it matches itself. The same applies to \* \+ \? \( \[ \{ \| \^ \$ \\ | example\.com |
\n | Line feed | Matches a newline character, code point 10. | line1\nline2 |
\r | Carriage return | Code point 13. Windows line endings are \r\n, so a pattern anchored with $ can trip on the stray \r. | \r\n |
\t | Tab | Matches a horizontal tab, code point 9. | name\tvalue |
\0 | Null character | Matches the null byte, code point 0. | \0 |
\xhh | Hex code point | Matches the character with the given two-digit hexadecimal code. | \x41 → A |
\uhhhh | Unicode code point | Matches a character by four-digit hex code point. | \u00e9 → é |
\u{hhhh} | Full code point | Matches a code point above U+FFFF as a single unit. Requires the u flag. | /\u{1F600}/u → 😀 |
\Q…\E | Literal block | Treats everything between as literal text. PCRE and Java only; in JavaScript escape each character instead. | \Qa.b*c\E |
Matches
Literal dot
Description
Escapes a metacharacter so it matches itself. The same applies to \* \+ \? \( \[ \{ \| \^ \$ \\
Example
example\.com
Matches
Line feed
Description
Matches a newline character, code point 10.
Example
line1\nline2
Matches
Carriage return
Description
Code point 13. Windows line endings are \r\n, so a pattern anchored with $ can trip on the stray \r.
Example
\r\n
Matches
Tab
Description
Matches a horizontal tab, code point 9.
Example
name\tvalue
Matches
Null character
Description
Matches the null byte, code point 0.
Example
\0
Matches
Hex code point
Description
Matches the character with the given two-digit hexadecimal code.
Example
\x41 → A
Matches
Unicode code point
Description
Matches a character by four-digit hex code point.
Example
\u00e9 → é
Matches
Full code point
Description
Matches a code point above U+FFFF as a single unit. Requires the u flag.
Example
/\u{1F600}/u → 😀
Matches
Literal block
Description
Treats everything between as literal text. PCRE and Java only; in JavaScript escape each character instead.
Example
\Qa.b*c\E
Flags & Modifiers (9)
Options that change how the whole pattern behaves.
| Token | Matches | Description | Example |
|---|---|---|---|
g | Global | Finds every match rather than stopping at the first. In JavaScript it also gives the regex a persistent lastIndex, so reusing one across test() calls alternates results. | /\d+/g |
i | Case-insensitive | Ignores case throughout the pattern, including inside character sets. | /hello/i → HELLO |
m | Multiline | Makes ^ and $ match at every line break instead of only at the ends of the input. | /^error/gm |
s | Dot matches all | Lets . match line breaks too. Equivalent to writing [\s\S] where the flag is unavailable. | /<div>.*<\/div>/s |
u | Unicode | Treats the pattern as code points rather than UTF-16 units, and enables \p{…} and \u{…}. Needed for emoji and non-Latin scripts. | /\p{Script=Tamil}+/u |
y | Sticky | Matches only at exactly lastIndex, never scanning forward. The flag tokenisers use to consume input strictly in order. | /\d+/y |
v | Unicode sets | A stricter successor to u that adds set operations such as intersection and difference inside character classes. | /[\p{L}--\p{Ascii}]/v |
d | Has indices | Adds match.indices giving the start and end offset of every group, not just its text. | /(\d+)/d |
(?i) | Inline flag | Turns a flag on partway through a pattern. Supported in PCRE, Python and Go; JavaScript has no inline flags. | (?i)hello |
Matches
Global
Description
Finds every match rather than stopping at the first. In JavaScript it also gives the regex a persistent lastIndex, so reusing one across test() calls alternates results.
Example
/\d+/g
Matches
Case-insensitive
Description
Ignores case throughout the pattern, including inside character sets.
Example
/hello/i → HELLO
Matches
Multiline
Description
Makes ^ and $ match at every line break instead of only at the ends of the input.
Example
/^error/gm
Matches
Dot matches all
Description
Lets . match line breaks too. Equivalent to writing [\s\S] where the flag is unavailable.
Example
/<div>.*<\/div>/s
Matches
Unicode
Description
Treats the pattern as code points rather than UTF-16 units, and enables \p{…} and \u{…}. Needed for emoji and non-Latin scripts.
Example
/\p{Script=Tamil}+/u
Matches
Sticky
Description
Matches only at exactly lastIndex, never scanning forward. The flag tokenisers use to consume input strictly in order.
Example
/\d+/y
Matches
Unicode sets
Description
A stricter successor to u that adds set operations such as intersection and difference inside character classes.
Example
/[\p{L}--\p{Ascii}]/v
Matches
Has indices
Description
Adds match.indices giving the start and end offset of every group, not just its text.
Example
/(\d+)/d
Matches
Inline flag
Description
Turns a flag on partway through a pattern. Supported in PCRE, Python and Go; JavaScript has no inline flags.
Example
(?i)hello
Common Patterns (12)
Ready-made patterns for everyday validation.
| Token | Matches | Description | Example |
|---|---|---|---|
^[^@\s]+@[^@\s]+\.[^@\s]+$ | Email (loose) | A deliberately permissive check: one @ with content either side and a dot in the domain. Strict RFC 5322 patterns reject valid addresses — confirm by sending mail instead. | [email protected] |
^https?:\/\/[^\s/$.?#].[^\s]*$ | URL | Matches an http or https URL with a plausible host. For anything load-bearing, parse with the URL constructor rather than a pattern. | https://example.com/a?b=1 |
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{12,}$ | Strong password | Stacked lookaheads require each character class independently, then the length is checked once. Length matters far more than composition — prefer 12 or more. | Tr0ub4dor&3xyz |
^\d{4}-\d{2}-\d{2}$ | ISO date | Matches the YYYY-MM-DD shape. It does not validate the date — 2026-02-31 passes, so parse afterwards. | 2026-06-09 |
^(?:\+91[\s-]?)?[6-9]\d{9}$ | Indian mobile | Ten digits starting 6–9, with an optional +91 country code. | +91 98765 43210 |
^[A-Z]{5}\d{4}[A-Z]$ | PAN number | The Indian Permanent Account Number format: five letters, four digits, one letter. | ABCDE1234F |
^(?:\d{1,3}\.){3}\d{1,3}$ | IPv4 (shape) | Checks the four-octet shape only — 999.1.1.1 passes. Add a range check, or validate with a parser. | 192.168.1.1 |
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$ | Hex colour | Matches three- or six-digit hex colours, with the hash optional. | #1e293b |
^[a-z0-9]+(?:-[a-z0-9]+)*$ | URL slug | Lowercase alphanumerics separated by single hyphens, with no leading or trailing hyphen. | git-commands-reference |
^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ | UUID | A canonical UUID with version 1–5 and a valid variant nibble. | 3f2504e0-4f89-11d3-9a0c-0305e82c3301 |
\s+$ | Trailing whitespace | Finds trailing spaces and tabs. With the m and g flags it cleans a whole file at once. | /\s+$/gm |
(\b\w+\b)\s+\1 | Repeated word | A backreference catching an accidentally doubled word — a proofreading pattern. | the the |
Matches
Email (loose)
Description
A deliberately permissive check: one @ with content either side and a dot in the domain. Strict RFC 5322 patterns reject valid addresses — confirm by sending mail instead.
Example
Matches
URL
Description
Matches an http or https URL with a plausible host. For anything load-bearing, parse with the URL constructor rather than a pattern.
Example
https://example.com/a?b=1
Matches
Strong password
Description
Stacked lookaheads require each character class independently, then the length is checked once. Length matters far more than composition — prefer 12 or more.
Example
Tr0ub4dor&3xyz
Matches
ISO date
Description
Matches the YYYY-MM-DD shape. It does not validate the date — 2026-02-31 passes, so parse afterwards.
Example
2026-06-09
Matches
Indian mobile
Description
Ten digits starting 6–9, with an optional +91 country code.
Example
+91 98765 43210
Matches
PAN number
Description
The Indian Permanent Account Number format: five letters, four digits, one letter.
Example
ABCDE1234F
Matches
IPv4 (shape)
Description
Checks the four-octet shape only — 999.1.1.1 passes. Add a range check, or validate with a parser.
Example
192.168.1.1
Matches
Hex colour
Description
Matches three- or six-digit hex colours, with the hash optional.
Example
#1e293b
Matches
URL slug
Description
Lowercase alphanumerics separated by single hyphens, with no leading or trailing hyphen.
Example
git-commands-reference
Matches
UUID
Description
A canonical UUID with version 1–5 and a valid variant nibble.
Example
3f2504e0-4f89-11d3-9a0c-0305e82c3301
Matches
Trailing whitespace
Description
Finds trailing spaces and tabs. With the m and g flags it cleans a whole file at once.
Example
/\s+$/gm
Matches
Repeated word
Description
A backreference catching an accidentally doubled word — a proofreading pattern.
Example
the the
Frequently Asked Questions
What is the difference between greedy and lazy quantifiers?
A greedy quantifier like .* takes as much as it can and gives characters back only if the rest of the pattern fails. Adding a ? makes it lazy — .*? takes as little as possible and expands only as needed. On <b>a</b><b>b</b>, <.*> matches the whole string while <.*?> matches just <b>.
How do I match a literal dot or slash?
Escape it with a backslash: \. matches a literal dot, and \/ a slash inside a /pattern/ literal. Inside a character class most specials lose their meaning, so [.] also matches a literal dot without an escape.
Should I validate email addresses with a regex?
Only loosely. The full RFC 5322 grammar is not worth expressing as a pattern, and strict regexes reject valid addresses. Check for one @ with something either side and a dot in the domain, then confirm the address by sending mail to it — that is the only real validation.
What does the g flag actually do?
It makes the regex find every match rather than stopping at the first. In JavaScript it also gives the object a lastIndex that persists between calls, so reusing a single /g/ regex across test() calls yields alternating results — create a fresh regex or reset lastIndex when that bites.