First off, sorry for the clickbait dog photo. I read my title after typing this up and it sounded way to dry. So, I added a cool picture of my dog to spice things up.
...onto the post.
If you're like me, Regex can sometimes elude you. I've been looking for a great course on the topic for almost 6 months now. Just something to make regexr.com actually useful...and not a site where I randomly try strings of code until something MAGICALLY works. 😬
Today, I found that course. And, I'm documenting my lessons here so that hopefully someone else can benefit.
In terms of using these functions, here are the most common js functions:
1) exec() About The Function
- This function returns an array or null. 2) test() About the function
- This function returns true/false if it was found 3) search() About the function
- This function returns the index the search finds the regex at or -1 if it doesn't find anything 4) replace() About the function
- This function returns a new string with the search being replaced.
The best course I found for this was a JS course by Brad Travery on Udemy. Here's the link. I highly recommend it. He covers some great ways to structure your JS files and more.
Top comments (7)
41: \d+ = 1 or more, not 0 or more (that would be * not +)
42: \D+ could be clearer as "1 or more non-digit characters"
Hint: When dealing with more complex RegEx in code; document each piece after it works in a comment in your code. It can be very helpful in the future when you've forgotten how/why it works, and as a reminder of cheats you've found in the past.
Also, beware different languages use slightly different optimizations/interpretations, so a wonderful RegEx in JS may do nothing useful in C++/Perl/C#/Java/Ruby/...
thanks for clarity on regular expressions, hopefully you could help I'm having trouble constructing a suitable expression that can accept University module codes that are in this format ("SCPS212", "CSPR111", "ADS213",...etc.) four letters or three and three numbers at the end.
I've tried pattern = "^[A-Z0-9]{6,7}$" and also tried hard coding them, it works that way but my lecture don't want me to hard code them.
Awesome cheatsheet! You should think about submitting a PR to github.com/LeCoupa/awesome-cheatsh... for this.
? and * are plain wrong, they aren't globbing, but quantifiers! *={0,} and ?={0,1}
Maybe you meant /h.*llo/i on line 16 because it won't match either 'heeeeeeeeeeeello' or 'heasdfasdfasdfllo'. Also, * means zero or more of the preceding character.
updated the gist. thanks
OMG! Great article and super useful to the masses (and myself). Thanks for writing this!