Toolstools.bryansuarez.dev

Regex cheatsheet

A searchable reference for JavaScript and PHP patterns.

Characters

  • .
    Any character
    Any character except a line break. With the s flag it matches line breaks too.
    a.c matches abc, a-c
  • \d
    Digit
    A digit 0 to 9.
    \d{4} matches 2025
  • \D
    Not a digit
    Any character that is not a digit.
  • \w
    Word character
    A letter, digit or underscore.
    \w+ matches user_1
  • \W
    Not a word character
    Anything except a letter, digit or underscore.
  • \s
    Whitespace
    Space, tab, line break and similar.
  • \S
    Not whitespace
    Any non-whitespace character.
  • \h
    Horizontal whitespacePHP only
    A space or tab.
  • \R
    Any line breakPHP only
    Matches \n, \r\n or \r as one unit.
  • \n \r \t
    Line feed, carriage return, tab
    Control characters.
  • \xhh
    Hex character
    The character with hex code hh.
    \x41 matches A
  • \uhhhh
    Unicode characterJavaScript only
    A character by its 4-digit code point.
    \u00e9 matches é
  • \x{hhhh}
    Unicode characterPHP only
    A character by code point. Needs the u flag.
    \x{1F600}
  • \p{L}
    Unicode letter
    Any letter in any language. Needs the u flag.
    \p{L}+ matches Ünïcode
  • \p{Lu}
    Uppercase letter
    Unicode uppercase letters. Needs the u flag.
  • \p{N}
    Unicode number
    Any numeric character. Needs the u flag.
  • \p{Emoji}
    EmojiJavaScript only
    Emoji characters. Needs the u flag.

Character sets

  • [abc]
    Any of
    One character from the set.
    [aeiou] matches vowels
  • [^abc]
    None of
    One character not in the set.
  • [a-z]
    Range
    Any character in the range.
    [0-9a-f] matches hex digits
  • [[:alpha:]]
    POSIX classPHP only
    Letters. Also :digit:, :alnum:, :space:, :upper:, :lower:, :punct:.

Anchors

  • ^
    Start
    Start of the text. With the m flag, start of each line.
  • $
    End
    End of the text. With the m flag, end of each line.
  • \b
    Word boundary
    Between a word character and a non-word character.
    \bcat\b matches cat, not concat
  • \B
    Not a word boundary
    Anywhere that is not a word boundary.
  • \A
    Start of subjectPHP only
    Very start of the text, ignoring the m flag.
  • \z
    End of subjectPHP only
    Very end of the text.
  • \Z
    End or before final newlinePHP only
    End of the text, allowing one trailing line break.

Quantifiers

  • *
    Zero or more
    Greedy: as many as possible.
    a* matches '', a, aaa
  • +
    One or more
    Greedy: at least one.
    \d+ matches 7, 2025
  • ?
    Optional
    Zero or one.
    colou?r matches color, colour
  • {n}
    Exactly n
    Exactly n times.
    \d{3}
  • {n,}
    n or more
    At least n times.
  • {n,m}
    Between n and m
    From n to m times.
    \d{2,4}
  • *? +? ??
    Lazy
    Add ? to match as few as possible.
    <.+?> matches one tag at a time
  • *+ ++ ?+
    PossessivePHP only
    Match greedily and never give back. Prevents backtracking.

Groups

  • (abc)
    Capture group
    Groups text and remembers the match as $1, $2 and so on.
    (\d+)-(\d+)
  • (?:abc)
    Non-capturing group
    Groups without remembering the match.
  • (?<name>abc)
    Named group
    Capture with a name. Reuse with \k<name>.
    (?<year>\d{4})
  • (?P<name>abc)
    Named group (PCRE)PHP only
    Older PCRE syntax. (?<name>) also works in PHP 5.2.2+.
  • \1 \k<name>
    Backreference
    Matches the same text as an earlier group.
    (\w)\1 matches aa, bb
  • a|b
    Alternation
    Matches a or b.
    cat|dog
  • (?>abc)
    Atomic groupPHP only
    Matches once and never backtracks into the group.
  • (?i)
    Inline flagPHP only
    Turn on a flag from this point, such as (?i) for case-insensitive.
  • (?R)
    RecursionPHP only
    Matches the whole pattern again, for nested structures.

Lookaround

  • (?=abc)
    Positive lookahead
    Followed by abc, without consuming it.
    \d+(?=px) matches 12 in 12px
  • (?!abc)
    Negative lookahead
    Not followed by abc.
  • (?<=abc)
    Positive lookbehind
    Preceded by abc.
    (?<=\$)\d+ matches 5 in $5
  • (?<!abc)
    Negative lookbehind
    Not preceded by abc.
  • \K
    Reset match startPHP only
    Drops everything matched so far from the result.
    foo\Kbar

Flags

  • i
    Ignore case
    Letters match in either case.
  • g
    GlobalJavaScript only
    Find all matches, not just the first. In PHP use preg_match_all.
  • m
    Multiline
    ^ and $ match at each line.
  • s
    Dot matches newline
    Lets . match line breaks.
  • u
    Unicode
    Treat the pattern and text as Unicode. Needed for \p{...}.
  • y
    StickyJavaScript only
    Match only at lastIndex.
  • d
    IndicesJavaScript only
    Report start and end positions of each group.
  • v
    Unicode setsJavaScript only
    Stricter Unicode mode with set operations in classes.
  • x
    ExtendedPHP only
    Ignore whitespace and allow # comments in the pattern.
  • U
    UngreedyPHP only
    Swaps greedy and lazy quantifiers.
  • D
    Dollar end onlyPHP only
    $ matches only at the very end.

Replacement

  • $1 $2
    Group reference
    Insert a captured group.
  • $<name>
    Named group referenceJavaScript only
    Insert a named group.
  • ${name}
    Named group referencePHP only
    Insert a named group.
  • $&
    Whole matchJavaScript only
    Insert the entire match.
  • $0 \0
    Whole matchPHP only
    Insert the entire match.
  • $$
    Literal dollarJavaScript only
    Insert a $ sign.

Common patterns

  • ^\S+@\S+\.\S+$
    Simple email
    A basic shape check. Fully validating email needs more than a regex.
  • ^\d{4}-\d{2}-\d{2}$
    ISO date
    Matches 2025-03-01. Does not check the date is real.
  • ^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
    Hex color
    Matches #fff and #1f5bd8.
  • ^https?://[^\s/$.?#].[^\s]*$
    URL
    A simple http or https link.
  • ^\s+|\s+$
    Trim whitespace
    Matches leading and trailing whitespace. Replace with nothing.
  • \s{2,}
    Repeated whitespace
    Two or more whitespace characters in a row.

Runs in your browser. Nothing you enter is sent anywhere.