A regular expression, or 'regex', is used to match parts of a string. Below is my cheat sheet for creating regular expressions.
Testing a regex...
For further actions, you may consider blocking this person and/or reporting abuse
I go cross-eyed dealing with regex, but this is an excellent reference I'm definitely coming back to. I almost always head to regex101.com whenever pattern-matching starts to devour my brain.
Just shared the link to regex101 with my colleagues today - great tool!
Yep, regex101 helps a lot! It even gives you the code snippet for your programming language (e.g. PHP)
RT
Using the
|
operator is a dangerous habit. You easily forget to guard yourself against matching substrings, for example consider the following line:And now what happens on
localhost.mydevious.url
? The secure solution is to use ^ and $ to gate the start and end of the tested string.Also, one trick up JS regex's sleeve is the back reference (
\1-\9
):This will re-use the
(matches)
inside the same regex.The real guard is to write tests to assert the correct behaviour :)
Just don't forget about those edge cases ;-)
The funny thing about these Regex cheat cheets is no matter how many of them I put in my bookmarks, I never end up using them.
That's why you need a regex cheat sheet mug!
sebastian 😂 me too
For me, Regex and Perl are write-only languages. I can't debug, edit or even read regex – I just rewrite it from scratch with some tool like RegExr or something. For me, a person who can memorise all that syntax is definitely a superhuman.
Making code readable is half the job of the coder. It's also true for regexes, which means you have to look for ways to separate parts and comment your regexes (or at least name the groups).
Fortunately, you can find solutions in most languages. Here are two examples from some of my OS codes:
In Javascript:
(I think there are JS libs to do that without taking my code, now)
In rust:
(this is the standard way in this language)
I tend to not bother learning regex, the cases where it's useful, I'll just go to a site like regexr.com and then use their tools to build one.
In think that's the general consensus. I only use it occasionally so it's not worth my time to be 100 percent fluent.
I'm using regex debuger, regexbuddy.com/, to debug different regex flavors. It saved me a lot of time over the years. It can also run on linux (using Wine).
They also have a tool for building regexps, regexmagic.com/, but I'm not using is so I'm just saying it is out there.
For the day-to_day development, I recommend using Rubular or Regexly
Thx a lot for this awesome cheat sheet !
You made a mistake in this case
The result of match is // ["E", "m", "m", "a", "1", "9", "3", "8", "2"] ;)
Great cheat sheet, thank you Emma! Soon I'm gonna make a post about this extension I made for VSCode that could help understanding RegExp: marketplace.visualstudio.com/items...
Oh my, I wish that I had encountered this post about two months ago before I began the difficult journey of acquiring what (little) regex knowledge I have now... haha!
I'm absolutely going to be bookmarking this to revisit next time I have the need and will be sharing it with my team. You've laid this out in a very understandable way; thanks for sharing :)
I <3 it :) thank you very much :)
very neat, thanks!
Great post and doubles as a great practice assessment if you hide all of the code blocks!
Thanks, this makes regex less scary! Keep up the good work
Pretty fine.
Shouldn't this also go under #regex?
⛄️
I love regexp 😊
Never ever did i wrote single regex without some kind of cheatsheet or tool. Thank you.
Nicely done :)
Always choose carefully when choosing to use a regular expression of course. Our primary application at work is for input validation, drawing heavily on the excellent work done in OWASP: owasp.org/index.php/OWASP_Validati...
Great post Emma!
I've mostly used .test, but you make good use of the .match method. Thanks for the great regex post with a JavaScript focus!
Best regex cheatsheet I ever came across - appletree.or.kr/quick_reference_ca...
This is a great reference guide. Thank you Emma
Thanks emma for the list, will share it in our JavaScript topics.
Cannot get enough of these :D... because regex is something that I have to lookup no matter how many times I use it.
Nicely explained....!
There are definitely some regex wizards out there, great work putting this together. 😁
I think you forgot to put what is the meaning of "gi" or "ig" in regular expression
For other folks out there
g modifier: global. All matches (don't return on first match)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
thanks for share this useful tips
Thank you for making such a clear reference
Thanks, very helpful...!
I love this, and what I really love is that you used super great and clear variable names!
Great cheat sheet! Regex tester for JavaScript: extendsclass.com/regex-tester.html
wow stuff !
i just bookmarked to save my time! great article
This is possibly the most save-worth article on all of dev.to. Definitely a must have resource, thank you so much!
Thanks for that aweseome post. Definitly worth to be bookmarked :-)
One hint: The commented output of "Match specific numbers and letters" is wrong.
Finally I understand hoe regular expressions works
Thanks, great cheat sheet...
Can anyone help me in writting regex for accepting google.com, yahoo.com, youtube.com but not sites.google.com translate.google.com m.youtube.com admin.yahoo.com etc
Thx for your great post. it helped me a lot. If you don't mind, Can I translate this post into Korean? Surely, there will be a link at top of my post directing to this original post.