Developer ReferenceUpdated: 2026-11-04

The Ultimate Regular Expressions (RegEx) Syntax & Patterns Cheat Sheet

Quick reference for character classes, anchors, quantifiers, lookarounds, flags, and tested production regex patterns.

Regular expressions (RegEx) are formal sequences of characters that define search patterns. Whether you are validating email inputs, extracting tokens from server logs, or parsing complex text structures, this reference card covers standard PCRE and JavaScript ECMAScript regex syntax with verified copy-paste patterns.

Character Classes & Shortcuts

Standard shorthand escape codes for matching common character sets.

CharacterMatchesExample Matches
.Any character except newline (unless `s` flag is set)`a`, `9`, `#`
\dAny decimal digit `[0-9]``5`, `0`, `8`
\DAny non-digit character `[^0-9]``a`, `Z`, `!`
\wWord character: alphanumeric plus underscore `[A-Za-z0-9_]``user_name1`
\WNon-word character `[^A-Za-z0-9_]``-`, `@`, `.`
\sWhitespace character (space, tab `\t`, newline `\n`)` `, `\t`, `\n`
\SNon-whitespace characterAny non-space glyph
[abc]Explicit set: matches `a`, `b`, or `c``a` in "cat"
[^abc]Negated set: matches any character EXCEPT `a`, `b`, or `c``d`, `x`, `1`
[a-z]Range: matches lowercase characters between `a` and `z``k`, `m`, `q`

Anchors & Boundaries

Specify the position in the string where a match must occur without consuming characters.

AnchorDescriptionCode Snippet
^Start of string or start of line in multiline mode (`m`)`^https?://`
$End of string or end of line in multiline mode (`m`)`\.pdf$`
\bWord boundary (transition between `\w` and `\W`)`\bcat\b` matches "cat" but not "catch"
\BNon-word boundary (within a word)`\Bcat\B` matches inside "scattered"

Quantifiers & Repetition

Specify how many times a character or subpattern should repeat. Suffix with `?` for non-greedy (lazy) matching.

QuantifierMeaningGreedy vs Lazy
*0 or more times (`{0,}`)`a*` matches "aaa" fully; `a*?` matches empty string
+1 or more times (`{1,}`)`a+` matches all "a"s; `a+?` matches the first "a"
?0 or 1 time (optional)`https?` matches "http" or "https"
{n}Exactly n occurrences`\d{4}` matches 4 consecutive digits
{n,}At least n occurrences`\w{3,}` matches 3 or more word chars
{n,m}Between n and m occurrences (inclusive)`\d{2,4}` matches 2 to 4 digits

Lookaheads & Lookbehinds (Zero-Width Assertions)

Assert whether a pattern is preceded or followed by another pattern without including it in the match result.

SyntaxTypeDescription & Example
(?=abc)Positive LookaheadAsserts pattern is followed by `abc`. `\d+(?=px)` matches "100" in "100px".
(?!abc)Negative LookaheadAsserts pattern is NOT followed by `abc`. `\d+(?!px)` matches digits not followed by "px".
(?<=abc)Positive LookbehindAsserts pattern is preceded by `abc`. `(?<=\$)\d+` matches "50" in "$50".
(?<!abc)Negative LookbehindAsserts pattern is NOT preceded by `abc`. `(?<!\$)\d+` matches numbers without dollar sign.

Verified Production RegEx Patterns

Tested, copy-ready regex snippets for common real-world software engineering tasks.

Email Address (RFC 5322 Standard)regex
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Validates standard email formatting across modern TLDs.
Strong Password Policy (Min 8 chars, 1 uppercase, 1 lowercase, 1 number, 1 special char)regex
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Ensures cryptographic complexity using positive lookaheads.
IPv4 Address Validationregex
^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Strict 0-255 octet validation without leading overflow.
URL with Protocol & Optional Port/Pathregex
^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$
Matches HTTP/HTTPS web links with query strings and hash anchors.
Hex Color Code (#RGB or #RRGGBB)regex
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Validates CSS color hex codes with or without hash prefix.
Slug Validation (URL-Friendly)regex
^[a-z0-9]+(?:-[a-z0-9]+)*$
Validates lowercase kebab-case slug strings without consecutive dashes.

Frequently Asked Questions

What is the difference between greedy and lazy matching?

By default, regex quantifiers (*, +, {n,}) are greedy: they consume as much text as possible while still allowing the pattern to match. Adding a question mark (?) turns them lazy (or non-greedy), causing them to consume as few characters as possible.

How do I test my regex patterns safely without exposing sensitive data?

Use DocSwift's 100% client-side Regex Tester. It runs directly inside your browser’s JavaScript runtime without sending test strings or tokens to any external server.