简体   繁体   中英

Convert a String to Spinal Case

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

function spinalCase(str) {
  var res = str.replace(/\s/g, "-")
  var result = res.replace(/_/g, '').toLowerCase();
  return result;
}

Code works only if there are spaces between strings or if there are no underscores. I am stuck trying to pass the rest of the test-cases, does anybody else have any tips or ideas in mind?

spinalCase("This Is Spinal Tap") should return "this-is-spinal-tap".
spinalCase("thisIsSpinalTap") should return "this-is-spinal-tap".
spinalCase("The_Andy_Griffith_Show") should return "the-andy-griffith-show".
spinalCase("Teletubbies say Eh-oh") should return "teletubbies-say-eh-oh".
spinalCase("AllThe-small Things") should return "all-the-small-things".

You may remove all non-alphanumeric chars at the start/end of the string, replace these consecutive chars with - anywhere else, then you may insert a hyphen in between lower- and uppercase letters, and then turn all to lower case.

 function spinalCase(str) { return str.replace(/^[\\W_]+|[\\W_]+$|([\\W_]+)/g, function ($0, $1) { return $1 ? "-" : ""; }).replace(/([az])(?=[AZ])/g, '$1-').toLowerCase(); } console.log(spinalCase("This Is Spinal Tap")); // "this-is-spinal-tap". console.log(spinalCase("thisIsSpinalTap")); // "this-is-spinal-tap". console.log(spinalCase("The_Andy_Griffith_Show")); // "the-andy-griffith-show". console.log(spinalCase("Teletubbies say Eh-oh")); //"teletubbies-say-eh-oh". console.log(spinalCase("AllThe-small Things")); // "all-the-small-things". 

Details

  • .replace(/^[\\W_]+|[\\W_]+$|([\\W_]+)/g, function ($0, $1) { return $1 ? "-" : ""; }) - removes all non-alphanumeric chars at the start ( ^[\\W_]+ )/end ( [\\W_]+$ ) of the string, replace these consecutive chars with - anywhere else ( ([\\W_]+) )
  • .replace(/([az])(?=[AZ])/g, '$1-') - insert a hyphen in between lower- and uppercase letters.

Answer for this question version : When input string use camel-case then we not need dictionary and can use regexp only:

 let s="exampleStringTwoThe-smallThing"; let r=s.replace(/([AZ][az\\-]*)/g, ' $1'); console.log(r); 

For current question version:

s.replace(/( |_)+/g,'-').replace(/([a-z])(?=[A-Z])/g, '$1-').toLowerCase()

 function spinalCase(s) { return s.replace(/( |_)+/g,'-').replace(/([az])(?=[AZ])/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". 

I improve solution by remove on replace using Wiktor answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM