Did a quick try and my final code does this (pseudocode):
Add space before each capital letter.
Change string to lowercase.
Replace non-letters with space.
Trim spaces (strings have such method).
Replace spaces with dashes.
There are ninjas who can write one line regexes, but I’m not one of them
@Icey You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
We have set your post to unlisted. Thanks for your understanding.
I updated your code. Here is my solution for the problem.
function spinalCase(str) {
var re = /(\ba-z)/g;
str = str.replace(re, function(x){return x.toUpperCase();});
var regex = /[-_+ |\s+]/g;
str = str.replace(regex, “”);
str = str.replace(/[A-Z]/g, function(match, offset) {
return (offset ? ‘-’ : ‘’) + match.toLowerCase();
});