📡 You're offline — showing cached content
New version available!
Quick Access
Regex Reference

Regex Mastery

Anchors, quantifiers, groups, lookaheads, and real-world patterns.

All Topics

Anchors & Boundaries

^ and $
^ anchors to start of string/line. $ anchors to end. Use with m flag for multi-line.
Example: /^Hello$/m // matches "Hello" on its own line
\b and \B
\b = word boundary (between \w and \W). \B = non-word boundary.
Example: /\bcat\b/ // "cat" but NOT "concatenate"
\A and \Z (PCRE)
Absolute start/end of entire string — unaffected by multiline flag (unlike ^ and $).
Example: /\Astart.*end\Z/s // Python/PHP strict anchoring
\G (continuation)
Matches only at the position where the previous match ended — for chained matching.
Example: /\G\d+,/ // matches each number+comma in a sequence

Quantifiers & Greedy vs Lazy

* + ? {n,m}
0+ / 1+ / 0 or 1 / between n and m. All are greedy — match as much as possible.
Example: /<.+>/ // greedy: matches entire "<b>text</b>"
*? +? ?? {n,m}?
Lazy (reluctant) quantifiers — match as LITTLE as possible. Add ? after any quantifier.
Example: /<.+?>/ // lazy: matches just "<b>"
*+ ++ (Possessive)
Possessive — matches greedily and never backtracks. Prevents catastrophic backtracking.
Example: /\d++[abc]/ // PCRE: no backtracking after digits match
{3} exactly / {3,} at least
Exact repetition / minimum repetition (no upper bound).
Example: /\d{4}-\d{2}-\d{2}/ // strict date format YYYY-MM-DD

Groups & Capturing

(abc) — Capturing group
Captures matched text accessible via $1/$2 or match[1]. Also used for alternation grouping.
Example: /(foo|bar)/ // captures "foo" or "bar" in group 1
(?:abc) — Non-capturing
Groups without capturing — useful for alternation when you don't need the group value.
Example: /(?:https?|ftp):\/\// // groups protocol options without capture
(?<name>abc) — Named group
Named capturing group — access by name instead of index. Improves readability.
Example: /(?<year>\d{4})-(?<month>\d{2})/.exec(s).groups.year
\1 \2 (Backreference)
Refer back to what was captured in group 1, 2 etc. inside the same pattern.
Example: /(['"])[^\1]*\1/ // match string with matching quotes
(?| branch reset)
PCRE branch reset group — alternation branches share the same capture group numbers.
Example: /(?|(cat)|(dog))/ // both alternatives are group 1
(?(1)yes|no) — Conditional
PCRE conditional: match "yes" pattern if group 1 was captured, else "no" pattern.
Example: /(<)?word(?(1)>)/ // optionally match word in angle brackets

Lookaheads & Lookbehinds

(?=abc) — Positive lookahead
Assert that what follows matches "abc" — without consuming characters (zero-width).
Example: /\d+(?= dollars)/ // matches "100" in "100 dollars"
(?!abc) — Negative lookahead
Assert that what follows does NOT match "abc" — great for password validation.
Example: /^(?!.*admin).*$/ // strings that don't contain "admin"
(?<=abc) — Positive lookbehind
Assert that what precedes matches "abc" — match after a prefix without including it.
Example: /(?<=\$)\d+/ // matches digits after a dollar sign
(?<!abc) — Negative lookbehind
Assert that what precedes does NOT match "abc".
Example: /(?<!\d)\d{4}/ // 4-digit num NOT preceded by another digit

Flags & Character Classes

g i m s u
Global / Case-insensitive / Multiline / Dot matches \n / Unicode mode (JS).
Example: /pattern/gim // all matches, case-insensitive, multi-line
\d \w \s \D \W \S
Digit / Word char / Whitespace — and their NEGATIONS (uppercase). Core building blocks.
Example: /^\w+@\w+\.\w{2,}$/ // simple email pattern
[abc] / [^abc] / [a-z0-9]
Character class / Negated class / Range. More precise than \w when you know the chars.
Example: /[a-zA-Z0-9_-]{3,20}/ // valid username pattern
\p{L} \p{N} (Unicode)
Unicode property escapes — match any Unicode letter/number (requires /u flag in JS).
Example: /\p{Emoji_Presentation}/gu // match emoji characters
(?x) verbose mode
Allows whitespace and comments in pattern for readability (PCRE/Python).
Example: re.compile(r"(?x) \d{4} # year \- \d{2} # month")

Real-World Patterns

Email validation
RFC-5321 simplified email regex — balance between strictness and practicality.
Example: /^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/
URL matching
Match http/https URLs with optional path/query/fragment.
Example: /https?:\/\/(www\.)?[-\w@:%.\+~#=]{1,256}\.[a-z]{2,6}\b[-\w@:%\+.~#?&=]*/i
Strong password
At least 8 chars, must have uppercase, lowercase, digit, and special char.
Example: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$/
IPv4 address
Match valid IPv4 addresses (0-255 in each octet).
Example: /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
Semantic Version
Match semver strings like 1.2.3, 1.0.0-alpha.1, 2.0.0+build.42.
Example: /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+([\w.-]+))?$/
Slug / URL-safe string
Validate or generate URL-friendly slugs (lowercase, hyphens, no spaces).
Example: /^[a-z0-9]+(?:-[a-z0-9]+)*$/ // "my-post-slug"