Ответ на этот версия вопроса : когда во входной строке используется верблюд , тогда нам не нужен словарь и мы можем использовать только регулярное выражение:
let s="exampleStringTwoThe-smallThing";
let r=s.replace(/([A-Z][a-z\-]*)/g, ' $1');
console.log(r);
Для текущей версии вопроса:
s.replace(/( |_)+/g,'-').replace(/([a-z])(?=[A-Z])/g, '$1-').toLowerCase()
function spinalCase(s) {
return s.replace(/( |_)+/g,'-').replace(/([a-z])(?=[A-Z])/g, '$1-').toLowerCase();
}
console.log( spinalCase("This Is Spinal Tap") ) // should return "this-is-spinal-tap".
console.log( spinalCase("thisIsSpinalTap") ) // should return "this-is-spinal-tap".
console.log( spinalCase("The_Andy_Griffith_Show") ) // should return "the-andy-griffith-show".
console.log( spinalCase("Teletubbies say Eh-oh") ) // should return "teletubbies-say-eh-oh".
console.log( spinalCase("AllThe-small Things") ) // should return "all-the-small-things".
Я улучшаю решение, удаляя на replace
, используя Wiktor answer