Files
2021-10-27 21:47:35 +05:30

2.5 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)的字符。 使用轉義字符有兩個原因:

  1. 首先是可以讓你使用無法輸入的字符,例如退格。
  2. 其次是可以讓你在一個字符串中表示多個引號,而不會出錯。

我們在之前的挑戰中學到了這個。

代碼輸出
\'單引號
\"雙引號
\\反斜槓
\n換行符
\r回車符
\t製表符
\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--

--after-user-code--

(function(){
if (myStr !== undefined){
console.log('myStr:\n' + myStr);}})();

--seed-contents--

const myStr = ""; // Change this line

--solutions--

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