Regex Cheatsheet
A quick reference for common regex syntax. Click any example to open the tester with it pre-loaded.
Character Classes
.Any CharacterMatches any single character (except newline, unless 's' flag)
a.c\dDigitMatches any digit (0–9)
\d+\DNon-DigitMatches any character that is NOT a digit
\D+\wWord CharacterMatches any word character (a–z, A–Z, 0–9, _)
\w+\WNon-Word CharacterMatches any non-word character
\W+\sWhitespaceMatches any whitespace (space, tab, newline)
\s+[abc]Character SetMatches any one of the listed characters
[aeiou][a-z]Character RangeMatches any character in the range
[a-z]+[^abc]Negated SetMatches any character NOT in the set
[^0-9]Anchors
^Start of StringMatches at the beginning of the string (or line in multiline)
^Hello$End of StringMatches at the end of the string (or line in multiline)
world$\bWord BoundaryMatches at a position between a word and non-word character
\bcat\b\BNon-Word BoundaryMatches at a position that is NOT a word boundary
\BcatQuantifiers
*Zero or MoreMatches 0 or more of the preceding element
ab*c+One or MoreMatches 1 or more of the preceding element
ab+c?OptionalMatches 0 or 1 of the preceding element
colou?r{n}Exactly nMatches exactly n of the preceding element
\d{3}{n,}n or MoreMatches n or more of the preceding element
\d{2,}{n,m}Between n and mMatches between n and m of the preceding element
\d{2,4}Groups
(abc)Capturing GroupGroups and captures the matched content
(\d+)-(\d+)(?:abc)Non-Capturing GroupGroups without capturing
(?:abc)+(?<name>)Named GroupCapturing group with a name
(?<year>\d{4})Lookaround
(?=abc)Positive LookaheadAsserts that what follows matches (without consuming)
\d(?=px)(?!abc)Negative LookaheadAsserts that what follows does NOT match
\d(?!px)(?<=abc)Positive LookbehindAsserts that what precedes matches
(?<=$)\d+(?<!abc)Negative LookbehindAsserts that what precedes does NOT match
(?<!$)\d+Flags
gGlobal FlagFind all matches, not just the first
/\d+/giCase-InsensitiveCase-insensitive matching
/hello/imMultiline^ and $ match at line boundaries
/^foo/msDotall. matches newlines too
/a.b/suUnicodeEnables Unicode mode
/\p{L}+/u