2020-09-29 22:09:05 +02:00

2.3 KiB

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
a103376db3ba46b2d50db289 Spinal Tap Case 5 16078 短线连接格式

Description

在这道题目中,我们需要写一个函数,把一个字符串转换为“短线连接格式”。短线连接格式的意思是,所有字母都是小写,且用-连接。比如,对于Hello World,应该转换为hello-world;对于I love_Javascript-VeryMuch,应该转换为i-love-javascript-very-much。 如果你遇到了问题,请点击帮助

Instructions

Tests

tests:
  - text: "<code>spinalCase('This Is Spinal Tap')</code>应该返回<code>'this-is-spinal-tap'</code>。"
    testString: assert.deepEqual(spinalCase("This Is Spinal Tap"), "this-is-spinal-tap");
  - text: "<code>spinalCase('thisIsSpinal<wbr>Tap')</code>应该返回<code>'this-is-spinal-tap'</code>。"
    testString: assert.strictEqual(spinalCase('thisIsSpinalTap'), "this-is-spinal-tap");
  - text: "<code>spinalCase('The_Andy_<wbr>Griffith_Show')</code>应该返回<code>'the-andy-griffith-show'</code>。"
    testString: assert.strictEqual(spinalCase("The_Andy_Griffith_Show"), "the-andy-griffith-show");
  - text: "<code>spinalCase('Teletubbies say Eh-oh')</code>应该返回<code>'teletubbies-say-eh-oh'</code>。"
    testString: assert.strictEqual(spinalCase("Teletubbies say Eh-oh"), "teletubbies-say-eh-oh");
  - text: "<code>spinalCase('AllThe-small Things')</code>应该返回<code>'all-the-small-things'</code>。"
    testString: assert.strictEqual(spinalCase("AllThe-small Things"), "all-the-small-things");

Challenge Seed

function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  return str;
}

spinalCase('This Is Spinal Tap');

Solution

function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  str = str.replace(/([a-z](?=[A-Z]))/g, '$1 ');
  return str.toLowerCase().replace(/\ |\_/g, '-');
}