DEV Community

Lakh Bawa
Lakh Bawa

Posted on

Easy to follow Regular Expression Cheatsheet

Hi Everyone, We know regular expressions can be tricky to read and write. I prepared this Regular expression cheatsheet with the help of various resources. I hope this helps.

Basic Matching

Character Meaning Example Matches
. Any single character h.t "hat", "hot", "hit"
* 0 or more of previous character go*gle "ggle", "google", "gooogle"
+ 1 or more of previous character go+gle "google", "gooogle"
? 0 or 1 of previous character colou?r "color", "colour"
^ Start of line ^Hello "Hello world" (not "say Hello")
$ End of line world$ "Hello world" (not "worldly")

Character Classes

Syntax Meaning Example Matches
[abc] Any single character in set [aeiou] Any vowel
[^abc] Any single character NOT in set [^0-9] Any non-digit
[a-z] Range of characters [0-9] Any single digit

Predefined Character Classes

Shorthand Equivalent Meaning
\d [0-9] Digit
\D [^0-9] Non-digit
\w [a-zA-Z0-9_] Word character
\W [^\w] Non-word character
\s [ \t\n\r\f\v] Whitespace
\S [^\s] Non-whitespace

Quantifiers

Syntax Meaning Example Matches
{n} Exactly n times a{3} "aaa"
{n,} n or more times a{2,} "aa", "aaa", "aaaa"
{n,m} Between n and m times a{2,4} "aa", "aaa", "aaaa"

Grouping & Capturing

Syntax Meaning Example Matches
(...) Capture group (ab)+ "ab", "abab", "ababab"
(?:...) Non-capturing group (?:ab)+ Same as above
\1, \2 Backreference (.)\1 "aa", "bb", "11"

Anchors & Boundaries

Syntax Meaning Example Matches
\b Word boundary \bcat\b "cat" (not "category")
\B Non-word boundary \Bcat\B "category"

Advanced Assertions

Syntax Meaning Example Matches
(?=...) Positive lookahead \d(?=px) "5" in "5px"
(?!...) Negative lookahead \d(?!px) "5" in "5em"
(?<=...) Positive lookbehind (?<=\$)\d+ "42" in "$42"
(?<!...) Negative lookbehind (?<!\$)\d+ "42" in "USD 42"

Common Use Case Patterns

Pattern Description Matches
`^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z a-z]{2,}$` Email
^\d{3}-\d{3}-\d{4}$ US Phone Number "123-456-7890"
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$ Strong Password Alphanumeric, 8+ chars

Regex Flags

Flag Meaning Example
i Case-insensitive /pattern/i
g Global match /pattern/g
m Multiline mode /pattern/m
s Dot matches newline /pattern/s

Performance Tips

  • Use non-capturing groups (?:...) when possible
  • Avoid backtracking
  • Use character classes instead of OR |
  • Be specific with quantifiers

Regex Test Tools

  • regex101.com
  • regexr.com
  • debuggex.com

Language Support

  • JavaScript: /pattern/flags
  • Python: re module
  • PHP: preg_ functions
  • Java: Pattern class
  • Ruby: // literals

Common Gotchas

  • . doesn't match newline
  • * and + are greedy
  • Escape special chars with \
  • Different syntax in different languages

Escape Special Characters

\ ^ $ . | ? * + ( ) [ ] { }
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Always test your regex with various inputs!

Please leave a like if you found this helpful. Thanks

Top comments (3)

Collapse
 
miguelneto profile image
Miguel Neto

Useful and straight to the point. Thank you.

Collapse
 
bawa_geek profile image
Lakh Bawa

Thank you @miguelneto

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Most useful image I ever downloaded:

Regex cheatsheet