[Intermediate Algorithm] - Spinal Tap Case

题目

将字符串转换为 spinal case。Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也就是以连字符连接所有小写单词。

提示

RegExp
String.replace()

测试用例

  • 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');

  

posted @ 2017-05-25 16:31  water-moon  阅读(190)  评论(0编辑  收藏  举报