Tell us what’s happening:
what is a shorter way to do this (better RegEx ?)
i feel like i used way too many variables when i could have somehow used 2
Your code so far
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
// create regex to check spaces or underlines against.
var a = /\s_/g;
//insert spaces before capital letters that are at word boundries, meaning not adding extra spaces before a word that already has one, or replacing letters with spaces. and then trim off spaces from before or after string
var b = str.replace(/\B([A-Z])/g, " $1").trim();
/*the '$1' , in a replace method , refers to the REPLACEMENT PATERN. as the MDN documentation says
$n Where n is a positive integer less than 100, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object. Note that this is 1-indexed.
*/
//turn string into lower case letters
var c = b.replace(/_/g,"").toLowerCase();
// call return statement that replaces regex in variable 'a' with dashes
return c.replace(/\s/g,'-');
}
spinalCase('The_Andy_Griffith_Show');
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36
.
Link to the challenge: