Cheatsheets
Regular expressions
Regex tokens — character classes, anchors, quantifiers, groups, and lookarounds.
43 entries
Character classes7
.Any character except newline
\dA digit (0-9)
\DA non-digit
\wA word character (a-z, A-Z, 0-9, _)
\WA non-word character
\sA whitespace character
\SA non-whitespace character
Anchors4
^Start of string / line
$End of string / line
\bA word boundary
\BNot a word boundary
Quantifiers7
*0 or more of the preceding token
+1 or more of the preceding token
?0 or 1 (optional)
*? +?Lazy (match as few as possible)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
Sets & groups8
[abc]Any one of a, b, or c
[^abc]Any character except a, b, c
[a-z]Any character in the range a-z
(...)Capturing group
(?:...)Non-capturing group
(?<name>...)Named capturing group
a|bMatch a or b
\1Backreference to group 1
Lookaround & escape5
(?=...)Positive lookahead
(?!...)Negative lookahead
(?<=...)Positive lookbehind
(?<!...)Negative lookbehind
\Escape the next metacharacter
Flags6
gGlobal — find all matches
iCase-insensitive
mMultiline — ^ and $ match each line
sDotall — . also matches newline
uUnicode mode
ySticky — match at lastIndex only
Common patterns6
^[\w.+-]+@[\w-]+\.[\w.-]+$Email (rough)
https?:\/\/[^\s]+URL
\b(?:\d{1,3}\.){3}\d{1,3}\bIPv4 address
^#?[0-9a-fA-F]{6}$Hex color
\d{4}-\d{2}-\d{2}ISO date (YYYY-MM-DD)
^[a-z0-9]+(?:-[a-z0-9]+)*$Slug