Leaderboard ad · 728×90

Regex Cheatsheet

Common regex syntax and examples. Copy patterns with one click and filter via search.

44 entries matched

Anchors4

^Start of string (start of each line with m flag)
$End of string (end of each line with m flag)
\bWord boundary
\BNon-word boundary

Character classes10

\dA digit, same as [0-9]
\DA non-digit
\wA word character, same as [A-Za-z0-9_]
\WA non-word character
\sA whitespace character
\SA non-whitespace character
.Any character (except newline; includes it with s flag)
[abc]Character set: matches a, b or c
[^abc]Negated set: any character except a, b or c
[a-z]Range: matches a to z

Quantifiers9

*0 or more of the previous item (greedy)
+1 or more of the previous item (greedy)
?0 or 1 of the previous item (greedy)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?0 or more (lazy, matches as few as possible)
+?1 or more (lazy)
??0 or 1 (lazy)

Groups & references5

(abc)Capturing group, referenced later by \1
(?:abc)Non-capturing group
\1Backreference to capture group 1
(?<year>\d{4})Named capture group
\k<year>Named backreference

Lookaround4

(?=abc)Positive lookahead: followed by abc
(?!abc)Negative lookahead: not followed by abc
(?<=abc)Positive lookbehind: preceded by abc
(?<!abc)Negative lookbehind: not preceded by abc

Flags6

gGlobal: find all matches
iCase-insensitive
mMultiline: ^ and $ match line boundaries
sDotall: . matches newlines too
uUnicode mode
ySticky: match at lastIndex only

Common examples6

[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,}Email address
https?:\/\/[^\s]+HTTP/HTTPS link
^((25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]?\d?\d)$IPv4 address
^1[3-9]\d{9}$Mainland China mobile number
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$Date YYYY-MM-DD
^#?([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$Hex color code
In-content ad · responsive