So suppose you want to remove the hyphen (-) in an AWS region name, you could write:
let region = ‘eu-west-1’;
let regionNoHyphen = region.split(‘-‘).join(”);
Much easier than using Regex, wouldn’t you agree?
I think once you get used to Regex in JS it isn’t too bad.
"y-e-p".replace(/-/g,"") // => yep
awesome! thank you for that, didn’t realise there’s a regex shorthand
Yeah, regex is great… if you want to replace a known substring. Now try writing a replaceall function which must be able to replace all possible substrings with another, regardless of the contents of that substring! Now you first have to escape your replace string, and then make sure your escape is correct, and then that it remains correct with later “improvements” to the JS’s regex specification…
You had a problem. You solved it with a regex. Now you have two problems.
The function in the article works for all strings, and doesn’t give you any headaches.