DevKit

Regex Cheat Sheet

Quick reference for regular expressions. Test patterns with our Regex Tester.

Character Classes

.Any character except newline
\wWord character [a-zA-Z0-9_]
\WNon-word character
\dDigit [0-9]
\DNon-digit
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Range: a to z

Anchors

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

Quantifiers

*0 or more (greedy)
+1 or more (greedy)
?0 or 1 (optional)
{3}Exactly 3
{3,}3 or more
{3,5}Between 3 and 5
*?0 or more (lazy)
+?1 or more (lazy)

Groups & Lookaround

(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
\1Backreference to group 1
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind

Flags

gGlobal — match all occurrences
iCase-insensitive
mMultiline — ^ and $ match line start/end
sDotall — . matches newline
uUnicode support

Common Patterns

^[\w.-]+@[\w.-]+\.\w{2,}$Email (simple)
^https?://[^\s]+$URL
^\d{4}-\d{2}-\d{2}$Date (YYYY-MM-DD)
^\+?\d{10,15}$Phone number
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$Hex color
^(?=.*[A-Z])(?=.*\d).{8,}$Strong password