DEV Community

Discussion on: Everything about RegEx and How to use it in JavaScript?

Collapse
 
andrewbridge profile image
Andrew Bridge

Really thorough run through of regex, such a useful feature once you know it!

Just as a heads up, your examples in your character groups section are a bit confusing. I think you've maybe missed the square brackets in the final two examples:

// Without character group
"abcdefghijklmnopqr".match(/defghij/g); 
// ["defghij"]

// With character group
"abcdefghijklmnopqr".match(/[defghij]/g); 
// ["d", "e", "f", "g", "h", "i", "j"]
Enter fullscreen mode Exit fullscreen mode
// Without character group
"1234567890".match(/4-9/g); 
// null

// With character group
"1234567890".match(/[4-9]/g); 
// ["4", "5", "6", "7", "8", "9"]
Enter fullscreen mode Exit fullscreen mode

With that said, the error shows how seemingly subtle changes can make a huge difference with regex, which is often where it becomes a bit tricky!

When I'm dealing with regexes that need to go into production code, I'll often set them up in a tool like Regex 101 and then use various different input strings (with multilines, odd characters etc) to check it's battle tested. I'll often then be able to put these into a unit test so the regex continues to work as expected in the long term.

Great tutorial once again!