DEV Community 👩‍đŸ’ģ👨‍đŸ’ģ

Cover image for The Regular Expression (RegEx) Cheat Sheet you always wanted
Tapajyoti Bose
Tapajyoti Bose

Posted on

The Regular Expression (RegEx) Cheat Sheet you always wanted

I get it even though Regex is incredibly useful, it is extremely hard to master! This is a cheat sheet that provides the most common RegEx use cases that will help you whenever need a sneak peek at the Regex syntax!


Characters Legend Example Sample Match
[abc], [a-c] Match the given characters/range of characters abc[abc] abca, abcb, abcc
[^abc], [^a-c] Negate and match the given characters/range of characters abc[^abc] abcd, abce, abc1, ...
. Any character except line break bc. bca, bcd, bc1, b., ...
\d Any numeric character (equivalent to [0-9]) c\d c1, c2, c3 ...
\D Any non-numeric character (equivalent to [^0-9]) c\D ca, c., c* ...
\w Any alphanumeric character (equivalent to [A-Za-z0-9_]) a\w aa, a1, a_ ...
\W Any non-alphanumeric character (equivalent to [A-Za-z0-9_]) a\W a), a$, a? ...
\s Usually used for white space, but can be used for new line, tab, etc a\s a
\S Not a white space or equivalent like new line, tab, etc a\S aa
\t Matches a horizontal tab T\tab T ab
\r Matches a carriage return AB\r\nCD AB
\n Matches a linefeed AB\r\nCD AB
\ Escapes special characters \d 0, 1, ...
x|y Matches either "x" or "y" a|b a, b


Characters Legend Example Sample Match
^ Start of string or start of line depending on multiline mode ^abc.* abc, abd, abcd, ...
$ End of string or start of line depending on multiline mode .*xyz$ xyz, wxyz, abcdxyz, ...
\b Matches a word character is not followed by another word-character My.*\bpie My apple pie, ...
\B Matches a non-word boundary c.*\Bcat copycat, ...
x(?=y) Lookahead assertion: Matches "x" only if "x" is followed by "y" \d+(?=â‚Ŧ) $1 = 0.98â‚Ŧ, ...
x(?!y) Negative Lookahead assertion: Matches "x" only if "x" is followed not by "y" \d+\b(?!â‚Ŧ) $1 = 0.98â‚Ŧ , ...
(?<=y)x Lookbehind assertion: Matches "x" only if "x" is preceded by "y" (?<=\d)\d $1 = 0.9*8*â‚Ŧ, ...
(?<!y)x Negative Lookbehind assertion: Matches "x" only if "x" is not preceded by "y" (?<!\d)\d $1 = 0.98â‚Ŧ, ...


Characters Legend Example Sample Match
(x) Capturing group: Matches x and remembers the match A(nt|pple) Ant (remembers "nt")
(?<name>x) Capturing group: Matches x and stores it in the mentioned variable A(?<m>nt|pple) Ant (m = "nt")
(?:name>x) Non-capturing group: Matches x and does not remember the match A(?:nt|pple) Ant
\n Back reference to the last substring matching the n parenthetical (\d)+(\d)=\2+\1 5+6=6+5


Characters Legend Example Sample Match
x* Matches the preceding item "x" 0 or more times a* a, aa, aaa, ...
x+ Matches the preceding item "x" 1 or more times, equivalent to {1,} a+ aa, aaa, aaaa, ...
x? Matches the preceding item "x" 0 or 1 time ab? a, ab
x{n} Matches the preceding item "x" n times (n = positive integer) ab{5}c abbbbbc
x{n,} Matches the preceding item "x" at least n times (n = positive integer) ab{2,}c abbc, abbbc, abbbbc, ...
x{n,m} Matches the preceding item "x" at least n times & at most m times (n<m) ab{2,3}c abbc, abbbc


By default quantifiers are greedy (they try to match as much of the string as possible). The ? character after the quantifier makes the quantifier non-greedy (it will stop as soon as it finds a match).

For Example: \d+? for a test string 12345 will match only 1, but \d+ will match the entire string 12345


Flags are put at the end of the regular expression. They are used to modify how the regular expression behaves.

For Example: /a/ for a test string a will match a only, but adding the flag i (/a/i) would match both a and A

Characters Legend
d Generate indices for substring matches
g Global search
i Case-insensitive search
m Multi-line search
s Allows . to match newline characters
u Treats a pattern as a sequence of Unicode code points
y Perform a sticky search that matches starting at the current position in the target string

That's all folks!

Research says, writing down your goals on pen & paper makes you 21% to 39% more likely to achieve them. Check out these notebooks and journals to make the journey of achieving your dreams easier:

Thanks for reading

Need a Top Rated Front-End Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

I have moved to Bali, Indonesia as a Digital Nomad. Follow me on Instagram to check out what I am up to.

Follow my blogs for Weekly new Tidbits on Dev


These are a few commonly asked questions I get. So, I hope this FAQ section solves your issues.

  1. I am a beginner, how should I learn Front-End Web Dev?
    Look into the following articles:

    1. Front End Development Roadmap
    2. Front End Project Ideas
  2. Would you mentor me?

    Sorry, I am already under a lot of workload and would not have the time to mentor anyone.

Top comments (15)

jonrandy profile image
Jon Randy

Most useful image I ever downloaded:

Image description

olawalemumeen2 profile image

Abeg where you download this

fjones profile image

As usual, I feel compelled to point out that you don't need most of these things and you don't want most of these things.

Especially readahead and lookbehind are features that add needless complexity and performance drawbacks (given an ideal implementation of the underlying algorithm).

My rule of thumb is usually: If you can't express it using exclusively concatenation, union, alternation, Kleene star, groups, and anchors, it probably shouldn't be done with regex.

This also ties into how I try to explain regex to people: A lot of the symbols are just shorthands for other symbol combinations:

a+ is the same as aa*. a? is the same as a|. Technically, even [ab] is just a|b. It's obviously a lot easier to write the shorthands - but the actual feature set you really need and want can all be boiled down into ^(|)*$ and that's wonderful. (Named and non-matching groups notwithstanding, of course.)

incrementis profile image
Akin C.

Hello Tapajyoti Bose,

thank you for your article.
I think I'll find it useful when I start working with regex again, as I'm no real expert.
I especially like how you added an example with a sample match for better understanding.

paulknulst profile image
Paul Knulst

Thanks for sharing this list.
Normally, I never know how to regex. Just opening and try different patterns.

devangtomar profile image
Devang Tomar

I so needed this.. thanks 🙌🎈

phlash profile image
Phil Ashby

Nice work, and of course the required XKCD:

As a corollary, the inimitable Jeff Atwood:


kasukur profile image

very well written with examples, thank you

dodov profile image
Hristiyan Dodov

I like Regex 101 because it has all of that information right where you can write and test your regex.

renancferro profile image
Renan C. Ferro

Woww, niice!

wilmela profile image


shshank profile image

Nice one, thanks for sharing.

olawalemumeen2 profile image

Thanks for providing this

sfritsch09 profile image
Sebastian Fritsch

BEST Cheatsheet for Regex: Regex Cheatsheet

mroeling profile image
Mark Roeling

One of the most complete regex documentation sites, and one that I use for many years:

🌚 Friends don't let friends browse without dark mode.

Good news! You can update to dark mode in your DEV settings.