Files
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/escape-sequences-in-strings.md
2023-04-14 08:37:21 -07:00

2.2 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244b6 转义字符 1 https://scrimba.com/c/cvmqRh6 17567 escape-sequences-in-strings

--description--

引号不是字符串中唯一可以被转义(escaped)的字符。 转义字符允许你使用可能无法在字符串中使用的字符。

代码输出
\'单引号
\"双引号
\\反斜杠
\n换行符
\t制表符
\r回车
\b退格符
\f换页符

请注意,反斜线本身必须被转义,才能显示为反斜线。

--instructions--

使用转义字符把下面三行文本赋值给一个变量 myStr

FirstLine
    \SecondLine
ThirdLine

你需要使用转义字符正确地插入特殊字符。 你也需要确保间距与上面文本一致,并且单词或转义字符之间没有空格。

注意:SecondLine 前面的空白是制表符,而不是空格。

--hints--

myStr 不能包含空格。

assert(!/ /.test(myStr));

myStr 应包含字符串 FirstLineSecondLineThirdLine(记得区分大小写)。

assert(
  /FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr)
);

FirstLine 后面应该是一个换行符 \n

assert(/FirstLine\n/.test(myStr));

myStr 应该包含一个制表符 \t,它在换行符后面。

assert(/\n\t/.test(myStr));

SecondLine 前面应该是反斜杠 \

assert(/\\SecondLine/.test(myStr));

SecondLineThirdLine 之间应该是换行符。

assert(/SecondLine\nThirdLine/.test(myStr));

myStr 应该只包含上面要求的字符。

assert(myStr === 'FirstLine\n\t\\SecondLine\nThirdLine');

--seed--

--seed-contents--

const myStr = ""; // Change this line

--solutions--

const myStr = "FirstLine\n\t\\SecondLine\nThirdLine";