Regex Cheat Sheet
Characters
. any \d digit \w word \s space
\D \W \S = the opposites
[abc] one of [^abc] none of [a-z0-9] ranges
\. \* \? \( \) escaped literals
Quantifiers and anchors
* 0+ + 1+ ? 0-1 {n} {n,} {n,m}
*? +? ?? lazy versions
^ start $ end \b word boundary
Groups and lookaround
(x) capture (?:x) group only (?<name>x) named
a|b either \1 backreference
x(?=y) followed by x(?!y) not followed by
(?<=y)x preceded by (?<!y)x not preceded by
JavaScript methods
re.test(s)
s.match(re); s.match(/x/g); [...s.matchAll(/x/g)]
s.replace(/x/g, "y"); s.replace(re, (m) => m.toUpperCase())
s.split(/[,;]\s*/)
new RegExp("^" + text + "$", "i")
Ready patterns
/^\d{6}$/ // 6-digit PIN
/^(?:\+91)?[6-9]\d{9}$/ // Indian mobile
/^[\w.+-]+@[\w-]+(\.[\w-]+)+$/ // email (basic)
/^[A-Z]{5}\d{4}[A-Z]$/ // PAN
/^#(?:[0-9a-f]{3}){1,2}$/i // hex colour
/^[a-z0-9]+(?:-[a-z0-9]+)*$/ // slug
Full Regex documentation