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