DEV Community

Cover image for A regex cheatsheet for all those regex haters (and lovers) 👀

A regex cheatsheet for all those regex haters (and lovers) 👀

catherine on January 10, 2019

My experience with regex I have always stayed far away from regex. In one of my first year computer science labs, there was an exercise ...
Collapse
 
frankusky profile image
Frank Sam Hu • Edited

I would like to share my two favorites tools to create, edit, visualize and debug regex:

  • Debuggex.com
    my favorite tool, it can make a diagram of how your regex will work, you can add multiple lines to test if the regex match the strings that you expect and also has a cheatsheet

  • RegExr.com
    Similar to debuggex except it doesnt generate the diagram, but in my opinion their cheatsheet is cleaner and easier to find what you need.

Collapse
 
dominik_gorczyca profile image
Dominik Gorczyca

Regex101 also seems like a great and quick site to know if your regex works!

Collapse
 
simov profile image
simo

Hey @catherinecodes , I think you've missed the greedy vs lazy matching:

// greedy
/".+"/.exec('a "witch" and her "broom" is one') // "witch" and her "broom"
// lazy
/".+?"/.exec('a "witch" and her "broom" is one') // "witch"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
catherinecodes profile image
catherine

Nice! I didn't learn those yet

Collapse
 
simov profile image
simo

You can achieve laziness with negated set too:

/"[^"]+"/.exec('a "witch" and her "broom" is one') // "witch"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ryan profile image
Ryan • Edited

Just to nitpick, the second wildcard example should be .*, and your group examples will fail because you didn't capture a space (it is ice cold outside and it is  cold outside match but not it is cold outside)

Regex is great! And not at all as hard as it looks. Although I still don't have the hang of lookahead/lookbehinds yet 😅

Collapse
 
catherinecodes profile image
catherine

Good catch Ryan! Thank you! I've updated the cheatsheet ☺️

I still need some more practice as well 😅lookaheads and lookbehinds were totally new to me! I didn't know about them during my CS lab 😲

Collapse
 
joemaffei profile image
Joe Maffei

You should add a note about lookbehind compatibility. It's not available in all browsers quite yet.

github.com/tc39/proposal-regexp-lo...
tc39.github.io/proposal-regexp-loo...

Collapse
 
mdebeus profile image
mdebeus

I am going to bookmark the Gist version of your very wonderful cheat sheet. I look forward to using it every time I work with regex. The samples appear in a very logical progression that makes it very easy to understand and use. Thank you!

Collapse
 
catherinecodes profile image
catherine

So happy you find it useful!!! ☺️☺️☺️

Collapse
 
madray profile image
Vic Danilov

Very nice and brainstorming regex challenge is RegexGolf - alf.nu/RegexGolf :)

Another view on "regex games" is Regex Crosswords. Very nice idea. regexcrossword.com/

Collapse
 
catherinecodes profile image
catherine

These look fun! Thanks for sharing :)

Collapse
 
guneyozsan profile image
Guney Ozsan • Edited

Regex = Black Magic
Convince me if not! Never sure why something works. I just don't touch it and put the script inside as many as folders possible in case some evil spirit leaks out to our world.

Collapse
 
catherinecodes profile image
catherine

Hide it away!!! 😂😂😂

Collapse
 
gjorgivarelov profile image
gjorgivarelov • Edited

Lookaheads and lookbehinds would be the features closest to "if-then" logic one uses while coding.
Just finished Bonnie Schulkin's course on regular expressions on Udemy. Great course, learned a lot about regex.

Collapse
 
mroeling profile image
Mark Roeling

Another really great resource is regular-expressions.info/

Collapse
 
nikoskip profile image
Nikolas

Nice post! I recommend this site too: regex101.com/

Collapse
 
sarthology profile image
Sarthak Sharma • Edited

Great post, Catherine. 👏🏼👏🏼

Created a gist out of this post. 😊

Link to Gist

Collapse
 
catherinecodes profile image
catherine

Thank you Sarthak! I'll link it in the post :)

Collapse
 
smh30 profile image
Stephanie Hope

Totally saving this, as I'm supposed to be learning regex next week in class!

Collapse
 
catherinecodes profile image
catherine

Yay, thank you! Have fun! :)

Collapse
 
technovice6 profile image
technovice6

In the ‘wildcards’ example, you’ve written

regex = /h.llo/; // the "" matches any character(s) zero or more times... matches
"hello", "heeeeeello", "hllo", "hwarwareallo"

How does it match "hwarwareallo" ? Does not the “.” indicate only one character?

Collapse
 
konkretnekosteczki profile image
KonkretneKosteczki

If you are adding usefull regex functions at the end of the code, you could also add information on replace function taking a callback as an argument.

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
p.replace('dog', (match, matchIndex, originalString) =>{
return match+"test"+matchIndex // match based replacer
})

Collapse
 
sesc profile image
Sebastian Schleussner • Edited

Another recommendation (not free though): Regular Expressions Cookbook

Collapse
 
dechamp profile image
DeChamp

Love this. I wish more people would take advantage of regex, it's so powerful! I love using regex101.com since it does a great job of explaining your regex as you write it out.

Collapse
 
joenewstrom profile image
Joe Newstrom

+1 to regexr.com

Great site... but now you have two problems.

Collapse
 
cxa profile image
CHEN Xian-an

Awesome! I also translated one Chinese copy: gist.github.com/cxa/901e1862cd9ddf...

Collapse
 
catherinecodes profile image
catherine

Cool! Thank you for this!

Collapse
 
aviloncho profile image
Juan Sebastian Avila

Thank u! It's a powerful tool!

Collapse
 
catherinecodes profile image
catherine

My pleasure :)

Collapse
 
brittanmcg profile image
Brittan McGinnis

Awesome post thank you!

Collapse
 
catherinecodes profile image
catherine

Glad you like it!

Collapse
 
cyr1l profile image
cyr1l

Hi, useful post!
Another online regex tester: extendsclass.com/regex-tester.html

Collapse
 
catherinecodes profile image
catherine

Thanks for sharing!

Collapse
 
thebeardedllama profile image
Steven Moschidis

not free, but RegexBuddy is awesome

that alone helped me to love regexs and made it super easy to deal with complex expressions

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

This is excellent, thank you! I hope you don't mind, I added it (with credit) to the "Development" portion of my company's wiki for quick reference next time one of us has to use Regex!

Collapse
 
nssimeonov profile image
Templar++

I would add "how to reverse the sides of an equation" to this list. There are a few other handy tricks for coders, but this is the most common I can think of right now

Collapse
 
sesc profile image
Sebastian Schleussner • Edited

Yes. If I understand you correctly, that would at the same time be a good example case for another detail I was about to suggest: Match-parenthesis references in the replacement.
Something like:
"a = b + c".replace(/^(.+?) *= *(.+)$/, "$2 = $1")

Collapse
 
jastewart profile image
jastewart

Sorry, but the terrible contrast with that black background makes this far too hard to read e.g. brown on black??

Collapse
 
codechunker profile image
codechunker

This is such an awesome article. thank you. There is also an article on regex for java developers. its awesome. dev.to/codechunker/introduction-to...

Collapse
 
primetiki profile image
{B}

I'm new to RegEx so thank you very much for taking the time to make this its a big help!

Collapse
 
techgeekbuzz profile image
Techgeekbuzz

Here (techgeekbuzz.com/regex-cheat-sheet/) we have also listed a complete checklist of cheat sheet, Please check.