JavaScript - Convert a string to spinal case



Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.



Solution Basic approach


function spinalCase(str) {
    return str
        .split(/\s|_|(?=[A-Z])/)
        .join('-')
        .toLowerCase()
}
console.log(spinalCase('ThisIsSpinalTap'));
console.log(spinalCase('This Is Spinal Tap'));
console.log(spinalCase('This_Is_Spinal_Tap'));

Output:


this-is-spinal-tap
this-is-spinal-tap
this-is-spinal-tap

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube