Regex Cheat Sheet
Complete reference for regular expression syntax β anchors, character classes, quantifiers, groups, lookarounds, flags and more. Click any symbol to test it in the playground.
Anchors
Start of string
Matches the beginning of the string (or line in multiline mode)
^hello"hello world" β matches "hello"
End of string
Matches the end of the string (or line in multiline mode)
world$"hello world" β matches "world"
Word boundary
Matches a position between a word and non-word character
\bcat\b"the cat sat" β matches "cat"
Non-word boundary
Matches a position that is NOT a word boundary
\Bcat\B"concatenate" β matches "cat"
Start of string (strict)
Matches only at the very start, never at line breaks
\Ahello"hello world" β matches "hello"
Character Classes
Any character
Matches any character except newline (use /s flag to include newline)
c.t"cat", "cot", "cut" β all match
Non-word character
Matches any character NOT in [a-zA-Z0-9_]
\W+"hello world!" β matches " " and "!"
Non-whitespace
Matches any character that is NOT whitespace
\S+"hello world" β matches "hello", "world"
Quantifiers
Zero or more (greedy)
Matches 0 or more of the preceding element, as many as possible
ab*c"ac", "abc", "abbc" β all match
One or more (greedy)
Matches 1 or more of the preceding element, as many as possible
ab+c"abc", "abbc" β match; "ac" β no match
Zero or one (greedy)
Matches 0 or 1 of the preceding element (makes it optional)
colou?r"color", "colour" β both match
Exactly n times
Matches exactly n occurrences of the preceding element
\d{4}"2024" β matches; "123" β no match
n or more times
Matches n or more occurrences of the preceding element
\d{2,}"12", "123", "1234" β all match
Between n and m times
Matches between n and m occurrences (inclusive)
\d{2,4}"12", "123", "1234" β match; "1" β no match
Zero or more (lazy)
Matches 0 or more, as few as possible
<.*?>"<a><b>" β matches "<a>" then "<b>"
Groups & References
Capture group
Groups the expression and captures the match for back-references
(\d{4})-(\d{2})"2024-05" β group 1: "2024", group 2: "05"
Non-capture group
Groups the expression without capturing (more efficient)
(?:abc)+"abcabc" β matches whole string
Named capture group
Captures with a name for easier reference
(?<year>\d{4})"2024" β group "year": "2024"
Back-reference
Refers to the text matched by capture group 1
(\w+) \1"hello hello" β matches; "hello world" β no match
Named back-reference
Refers to the text matched by a named capture group
(?<w>\w+) \k<w>"hello hello" β matches
Lookahead & Lookbehind
Positive lookahead
Matches a position followed by the pattern (without consuming it)
\d+(?= dollars)"100 dollars" β matches "100"
Negative lookahead
Matches a position NOT followed by the pattern
\d+(?! dollars)"100 euros" β matches "100"
Positive lookbehind
Matches a position preceded by the pattern (without consuming it)
(?<=\$)\d+"$100" β matches "100"
Character Sets
Negated set
Matches any single character NOT in the set
[^aeiou]"hello" β matches "h", "l", "l"
Flags / Modifiers
Global
Find all matches instead of stopping after the first one
/\d+/g"a1b2c3" β matches "1", "2", "3"
Multiline
^ and $ match start/end of each line, not just the string
/^\w+/gmMatches first word on each line
Unicode
Enables full Unicode matching and support for \u{} escapes
/\u{1F600}/uMatches emoji and Unicode chars properly
Sticky
Matches only from the position indicated by lastIndex property
/\d+/yAnchors match to lastIndex position
Special & Escape
Alternation
Matches either the expression before or after the |
cat|dog"I have a cat" or "I have a dog" β match
Ready to test a pattern?
Open the playground, paste any regex from this cheat sheet, and see real-time matches with visual highlighting.
Open Playground