Convert string to spinal case Due to an excercise, I've made a function that converts given string to spinal case just-like-this-one (all lower case).
I did it with two regular expressions. Can it be done with one, or generally better?



function spinalCase(str) {
  //if string is not delimited 
  if(str.search(/[_\s]/) === -1){
    // insert dash before every uppercase letter
    return str.replace(/(?=[A-Z])/g , '-').toLowerCase();
  } else {    
    //change delimiters to dashes  
    return str.replace(/[_\s](?=[a-zA-Z])/g, '-').toLowerCase();
  }
}




edit:
variable naming was forced by excercise (wouldn't complete otherwise).
Example converted input:

"This Is Spinal Tap"

"thisIsSpinalTap"

"The_Andy_Griffith_Show"

"Teletubbies say Eh-oh"
This does it in two steps:


Add spaces before each uppercase, which allows the uppercases to be handled by...
The delimiter case.  This is the same regex you already wrote, although I am collapsing multiple delimters down to a single one.


Having two transformation passes, similar to using a unix pipeline, is easier to reason about that breaking it up into two cases with an "if else".

Note the cryptic (?!^) matches "not at the beginning of a line" only

function spinalCase(str) {
  return str.replace(/(?!^)([A-Z])/g, ' $1')
            .replace(/[_\s]+(?=[a-zA-Z])/g, '-').toLowerCase();
}


All of your test cases pass:

this-is-spinal-tap
this-is-spinal-tap
the-andy-griffith-show
teletubbies-say-eh-oh