[中間アルゴリズム]-スパイナルタップケース
Spinal Tap Case
トピック
文字列を変換するにはspinal case
。大文字と小文字は、この形式ではすべて小文字の単語がダッシュで結合され、すべて小文字の単語でハイフンでつながれています。
促す
テストケース
spinalCase('This Is Spinal Tap')
戻る必要があります'this-is-spinal-tap'
。spinalCase('thisIsSpinalTap')
戻る必要があります'this-is-spinal-tap'
。spinalCase('The_Andy_Griffith_Show')
戻る必要があります'the-andy-griffith-show'
。spinalCase('Teletubbies say Eh-oh')
戻る必要があります'teletubbies-say-eh-oh'
。
アイデアの分析
コード
1.function spinalCase(str) { 2. // 'It's such a fine line between stupid, and clever.' 3. // --David St. Hubbins 4. 5. return str.replace(/[^A-Za-z]/g, ' ') 6. .replace(/([A-Z])/g, ' $1') 7. .replace(/^s/g, '') 8. .replace(/s+/g, '-') 9. .toLowerCase() 10.} 11. 12.spinalCase('This Is Spinal Tap')