Regex Cheat Sheet — Complete Pattern Reference

May 26, 20266 min read

Character Classes

PatternMeaning
.Any character except newline
\dDigit [0-9]
\DNon-digit [^0-9]
\wWord character [a-zA-Z0-9_]
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any character in set
[^abc]Any character NOT in set
[a-z]Character range

Quantifiers

PatternMeaning
*Zero or more
+One or more
?Zero or one (optional)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?Zero or more (lazy)
+?One or more (lazy)

Anchors

PatternMeaning
^Start of string/line
$End of string/line
\bWord boundary
\BNon-word boundary

Groups and References

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

Flags

FlagMeaning
gGlobal — find all matches
iCase-insensitive
mMultiline — ^/$ match line boundaries
sDotall — . matches newline
uUnicode support

Common Patterns

  • Email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  • URL: /^https?:\/\/.+/
  • IP Address: /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/

See 20 Useful Regex Patterns for more examples.

Try It Yourself

Use our free Regex Tester to test and debug regex patterns in real time.