freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/escaping-literal-quotes-in-...

73 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
id: 56533eb9ac21ba0edf2244b5
title: 转义字符串中的引号
challengeType: 1
videoUrl: 'https://scrimba.com/c/c2QvgSr'
forumTopicId: 17568
dashedName: escaping-literal-quotes-in-strings
---
# --description--
定义一个字符串必须要用单引号或双引号来包裹它。 那么当你的字符串里面包含引号 `"` 或者 `'` 时该怎么办呢?
在 JavaScript 中,可以通过在引号前面使用<dfn>反斜杠</dfn>`\`)来<dfn>转义</dfn>引号。
```js
var sampleStr = "Alan said, \"Peter is learning JavaScript\".";
```
有了转义符号JavaScript 就知道这个单引号或双引号并不是字符串的结尾,而是字符串内的字符。 所以,上面的字符串打印到控制台的结果为:
```js
Alan said, "Peter is learning JavaScript".
```
# --instructions--
使用<dfn>反斜杠</dfn>`myStr` 变量赋值一个字符串,这样如果你要打印它到控制台,将会看到:
```js
I am a "double quoted" string inside "double quotes".
```
# --hints--
你的代码中应该包含两个双引号(`"`)以及四个转义的双引号(`\"`)。
```js
assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
```
变量 myStr 应该包含字符串 `I am a "double quoted" string inside "double quotes".`
```js
assert(/I am a "double quoted" string inside "double quotes(\."|"\.)$/.test(myStr));
```
# --seed--
## --after-user-code--
```js
(function(){
if(typeof myStr === 'string') {
console.log("myStr = \"" + myStr + "\"");
} else {
console.log("myStr is undefined");
}
})();
```
## --seed-contents--
```js
var myStr = ""; // Change this line
```
# --solutions--
```js
var myStr = "I am a \"double quoted\" string inside \"double quotes\".";
```