Hello campers of young and old, today, we are going to be converting strings to spinal-tab-case. If you did not know about spinal-tab-case, well, you do now. You just-saw-it-twic… thrice!
Spinal case simply removes all spaces and sets dashes instead, while having the string be all lower-case characters. It sounds easy to achieve, we are replace wizards after all; but we’ll see that some cases might not be so straightforward.
First thing that comes to mind is probably:
function spinalCase(str) { return str.replace(/ /g, '-').toLowerCase(); }
We first replace all spaces with dashes, and then get rid of capital letters. And you see, that works for strings such as:
spinalCase('Hello world!'); //-> hello-world! spinalCase('I want to see the world BURN.'); //-> i-want-to-see-the-world-burn.
But how about these other case?
spinalCase('Hey_I_got_you_this_time'); //-> hey_I_got_you_this_time
Let’s get rid of those underscores too!