2.0 KiB
2.0 KiB
id, challengeType, videoUrl, forumTopicId, title
id | challengeType | videoUrl | forumTopicId | title |
---|---|---|---|---|
56533eb9ac21ba0edf2244b5 | 1 | https://scrimba.com/c/c2QvgSr | 17568 | 转义字符串中的引号 |
Description
"
或者'
时该怎么办呢?
在 JavaScript 中,你可以通过在引号前面使用反斜杠(\
)来转义引号。
var sampleStr = "Alan said, \"Peter is learning JavaScript\".";
有了转义符号,JavaScript 就知道这个单引号或双引号并不是字符串的结尾,而是字符串内的字符。所以,上面的字符串打印到控制台的结果为:
Alan said, "Peter is learning JavaScript".
Instructions
myStr
,打印到控制台,输出为:
I am a "double quoted" string inside "double quotes".
Tests
tests:
- text: 你的代码中应该包含两个双引号 (<code>"</code>) 以及四个转义的双引 (<code>\"</code>)。
testString: assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
- text: 变量 myStr 应该包含字符串<code>I am a "double quoted" string inside "double quotes".</code>。
testString: assert(myStr === "I am a \"double quoted\" string inside \"double quotes\".");
Challenge Seed
var myStr = ""; // Change this line
After Test
(function(){
if(typeof myStr === 'string') {
console.log("myStr = \"" + myStr + "\"");
} else {
console.log("myStr is undefined");
}
})();
Solution
var myStr = "I am a \"double quoted\" string inside \"double quotes\".";