tests:
- text: <code>spinalCase("This Is Spinal Tap")</code> should return <code>"this-is-spinal-tap"</code>.
testString: assert.deepEqual(spinalCase("This Is Spinal Tap"), "this-is-spinal-tap");
- text: <code>spinalCase("thisIsSpinal<wbr>Tap")</code> should return <code>"this-is-spinal-tap"</code>.
testString: assert.strictEqual(spinalCase('thisIsSpinalTap'), "this-is-spinal-tap");
- text: <code>spinalCase("The_Andy_<wbr>Griffith_Show")</code> should return <code>"the-andy-griffith-show"</code>.
testString: assert.strictEqual(spinalCase("The_Andy_Griffith_Show"), "the-andy-griffith-show");
- text: <code>spinalCase("Teletubbies say Eh-oh")</code> should return <code>"teletubbies-say-eh-oh"</code>.
testString: assert.strictEqual(spinalCase("Teletubbies say Eh-oh"), "teletubbies-say-eh-oh");
- text: <code>spinalCase("AllThe-small Things")</code> should return <code>"all-the-small-things"</code>.
testString: assert.strictEqual(spinalCase("AllThe-small Things"), "all-the-small-things");
Offical soln:
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
str = str.replace(/([a-z](?=[A-Z]))/g, '$1 ');
return str.toLowerCase().replace(/\ |\_/g, '-');
}Other solns:
Spinal Tap Case - Hugh Winchester
identifying where there is difference in case, and inserting a dash in between those two characters.
function spinalCase(str) {
return str.replace(/([a-z])([A-Z])/g, ‘$1-$2’)
.replace(/\s+|_+/g, ‘-’)
.toLowerCase();
//returns 'this-is-spinal-tap'
}
spinalCase(‘This Is_Spinal Tap’);function spinalCase(str) {
// Create a variable for the white space and underscores.
var regex = /\s+|_+/g;
// Replace low-upper case to low-space-uppercase
str = str.replace(/([a-z])([A-Z])/g, '$1 $2');
// Replace space and underscore with -
return str.replace(regex, '-').toLowerCase();
}repl.it/@iyeradith/Spinal-Tap-Case
function spinalCase(str)
{
return str.split(/\s+|_+|(?=[A-Z])/).join("-").toLowerCase();
}
spinalCase('This Is Spinal___Tap');Regular Expressions: Use Capture Groups to Search and Replace