--- id: 56533eb9ac21ba0edf2244b6 challengeType: 1 videoUrl: 'https://scrimba.com/c/cvmqRh6' forumTopicId: 17567 title: 字符串中的转义序列 --- ## Description
引号不是字符串中唯一可以被转义的字符。使用转义字符有两个原因:首先是可以让你使用无法输入的字符,例如退格。其次是可以让你在一个字符串中表示多个引号,而不会出错。我们在之前的挑战中学到了这个。
代码输出
\'单引号
\"双引号
\\反斜杠
\n换行符
\r回车符
\t制表符
\b退格
\f换页符
请注意,必须对反斜杠本身进行转义才能显示为反斜杠。
## Instructions
使用转义字符将下面三行文本字符串赋给变量myStr
FirstLine
    \SecondLine
ThirdLine
你需要使用转义字符正确地插入特殊字符,确保间距与上面文本一致并且单词或转义字符之间没有空格。 像这样用转义字符写出来: FirstLine换行符制表符反斜杠SecondLine换行符ThirdLine
## Tests
```yml tests: - text: myStr不能包含空格。 testString: assert(!/ /.test(myStr)); - text: myStr应该包含字符串FirstLine, SecondLine and ThirdLine (记得区分大小写)。 testString: assert(/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr)); - text: FirstLine后面应该是一个新行\n。 testString: assert(/FirstLine\n/.test(myStr)); - text: myStr应该包含制表符\t并且制表符要在换行符后面。 testString: assert(/\n\t/.test(myStr)); - text: SecondLine前面应该是反斜杠\\。 testString: assert(/\SecondLine/.test(myStr)); - text: SecondLineThirdLine之间应该是换行符。 testString: assert(/SecondLine\nThirdLine/.test(myStr)); - text: myStr 应该只包含介绍里面展示的字符串。 testString: assert(myStr === 'FirstLine\n\t\\SecondLine\nThirdLine'); ```
## Challenge Seed
```js var myStr; // Change this line ```
### After Test
```js (function(){ if (myStr !== undefined){ console.log('myStr:\n' + myStr);}})(); ```
## Solution
```js var myStr = "FirstLine\n\t\\SecondLine\nThirdLine"; ```