Back to Blog
    Documentation8 min readJanuary 5, 2026

    Regex Cheat Sheet: Common Patterns & Syntax

    A quick reference and guide for Regular Expressions (Regex). matching characters, quantifiers, groups, lookarounds, and flags.

    Try the Regex Tester

    Put what you learn into practice

    Introduction

    Regular Expressions (Regex) are a sequence of characters that define a search pattern. They are the Swiss Army knife of text processing, allowing you to validate forms, scrape data, or perform complex find-and-replace operations.

    While the syntax can look cryptic (^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$), mastering just a few concepts gives you superpowers.

    Character Classes

    Character classes match a specific type of character.

    PatternMatchesExample
    .Any character (except newline)a.c matches "abc", "a@c"
    \dAny digit (0-9)user_\d matches "user_5"
    \DAny non-digit\D+ matches "abc"
    \wAlphanumeric + underscore\w+ matches "User_123"
    \WNon-alphanumeric\W matches "!" or "@"
    \sWhitespace (space, tab, newline)hello\sworld
    \SNon-whitespace\S+ matches "word"

    Anchors

    Anchors don't match characters; they match positions.

    PatternMatches
    ^Start of the string (or line in multiline mode)
    $End of the string (or line in multiline mode)
    \bWord boundary (start/end of word)
    \BNon-word boundary

    Example:

    • ^hello matches "hello world" but not "oh hello".
    • world$ matches "hello world" but not "world peace".

    Quantifiers

    Quantifiers specify how many times the previous character/group should repeat.

    PatternMeaningExample
    *0 or morea*b matches "b", "ab", "aaaaab"
    +1 or morea+b matches "ab", "aaab" (not "b")
    ?0 or 1 (Optional)colou?r matches "color", "colour"
    {n}Exactly n times\d{4} matches "2026"
    {n,}n or more times\d{2,} matches "12", "12345"
    {min,max}Between min and max\w{3,8} matches "User", "Username"

    Greedy vs Lazy: By default, quantifiers are greedy—they match as much as possible. Adding a ? makes them lazy.

    • String: <div>content</div>
    • <.+> (Greedy): matches <div>content</div> (entire string)
    • <.+?> (Lazy): matches <div> and </div> separately.

    Groups and Lookarounds

    Capturing Groups (...)

    Groups parts of the regex and captures the match for later use (e.g., in replacement).

    • Pattern: (\d{4})-(\d{2}) on "2026-01"
    • Group 1: "2026"
    • Group 2: "01"

    Non-Capturing Groups (?:...)

    Groups for logic/quantifiers but doesn't capture the result.

    • (?:ma)+ matches "mama" but doesn't create a capture group.

    Lookahead (?=...)

    Asserts that what follows matches the pattern, but doesn't consume characters.

    • d+(?= dollars) matches "100" only in "100 dollars".

    Lookbehind (?<=...)

    Asserts that what precedes matches the pattern.

    • (?<=USD)d+ matches "100" only in "USD100".

    Flags

    Flags change how the regex engine behaves. They are appended to the end (e.g., /pattern/gi).

    FlagNameDescription
    gGlobalMatch all occurrences, don't stop at first.
    iInsensitiveCase-insensitive matching.
    mMultiline^ and $ match start/end of lines, not just string.
    sDotall. matches newlines as well.
    uUnicodeEnable full Unicode support.

    Common Patterns

    Email Address (Simplified)

    ^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$
    

    URL (HTTP/HTTPS)

    https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
    

    Date (YYYY-MM-DD)

    \d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])
    

    Conclusion

    Regex is a language of its own. It's often write-only (hard to read later), so commenting your complex regexes or splitting them up is good practice. Use our Regex Tester to debug your patterns in real-time with visual highlighting!

    Regexcheat sheetJavaScriptdocumentationparsing

    Related Articles