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 ... [Read Full]
markdown guide
 

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.

 

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"
 
 

You can achieve laziness with negated set too:

/"[^"]+"/.exec('a "witch" and her "broom" is one') // "witch"
 

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 πŸ˜…

 

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 😲

 

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!

 

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

 

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

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

 
 

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...

 

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
})

 

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.

 
 
 
 

Great post, Catherine. πŸ‘πŸΌπŸ‘πŸΌ

Created a gist out of this post. 😊

Link to Gist

 
 
 

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.

 
 
 
 
 
 

+1 to regexr.com

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

 

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!

 

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

 

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")

 

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

 

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

 

not free, but RegexBuddy is awesome

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

code of conduct - report abuse