Regex can be a powerful tool for working with text, but it can also be complex and difficult to master. This cheat sheet provides a quick reference for some of the most common regex patterns and syntax.



. Matches any character except line breaks = [^\n\r]
\w Matches any word character =[A-Za-z0-9_]
\d Matches any digit character = [0-9]
\s Matches any whitespace character (spaces, tabs, line breaks).


* 0 or more = {0,}
+ 1 or more = {1,}
{n} exactly n
{n,} n or more
{,n} n or less
? 0 or 1 = {0,1}

Word Boundary

\bsoheil\b finds all soheil words, which means there should be a soheil that is not attached to any word character ([A-Za-z0-9_]). Before and after soheil could be a space, dot or any other non-word character.


  • g If global is enabled it searches for the next matches after the end of each match. Without g flag, it finds the first match only.
  • i Makes the whole expression case-insensitive. For example, /aBc/i would match AbC.
  • m When the multiline flag is enabled, beginning and end anchors (^ and $) will match the start and end of a line, instead of the start and end of the whole string.

Greedy vs Lazy

.+ Greedy, finds the biggest subset
.+? Lazy, finds the smallest subset


Assume you want to replace all the names with the bold version: *Soheil* *Alice* *Bob*. You can use \*(\w+)\* to find all the words between asterisks, and <b>$1</b> to replace names.

$1 actually references back to the first matched group which is matched area inside the parenthesis (\w+).


(.|[\r\n])+ matches 1 or more any character and new line
p {(.|[\r\n])+?} finding p rules in CSS
\.banner {(.|[\r\n])+img { finding img inside banner class for sass or less