2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: a6b0bb188d873cb2c8729495
|
|
|
|
title: Convert HTML Entities
|
|
|
|
challengeType: 5
|
|
|
|
localeTitle: 转换HTML实体
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-09-07 16:10:29 +08:00
|
|
|
<section id='description'>
|
|
|
|
在这道题目中,我们需要写一个转换 HTML entity 的函数。需要转换的 HTML entity 有<code>&</code>、<code><</code>、<code>></code>、<code>"</code>(双引号)和<code>'</code>(单引号)。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-09-07 16:10:29 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: "<code>convertHTML('Dolce & Gabbana')</code>应该返回<code>Dolce &​amp; Gabbana</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.match(convertHTML("Dolce & Gabbana"), /Dolce & Gabbana/);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: "<code>convertHTML('Hamburgers < Pizza < Tacos')</code>应该返回<code>Hamburgers &​lt; Pizza &​lt; Tacos</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.match(convertHTML("Hamburgers < Pizza < Tacos"), /Hamburgers < Pizza < Tacos/);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: "<code>convertHTML('Sixty > twelve')</code>应该返回<code>Sixty &​gt; twelve</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.match(convertHTML("Sixty > twelve"), /Sixty > twelve/);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: "<code>convertHTML('Stuff in \"quotation marks\"')</code>应该返回<code>Stuff in &​quot;quotation marks&​quot;</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.match(convertHTML('Stuff in "quotation marks"'), /Stuff in "quotation marks"/);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: "<code>convertHTML('Schindler's List')</code>应该返回<code>Schindler&​apos;s List</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.match(convertHTML("Schindler's List"), /Schindler's List/);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: "<code>convertHTML('<>')</code>应该返回<code>&​lt;&​gt;</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.match(convertHTML('<>'), /<>/);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: "<code>convertHTML('abc')</code>应该返回<code>abc</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.strictEqual(convertHTML('abc'), 'abc');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function convertHTML(str) {
|
|
|
|
// :)
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
convertHTML("Dolce & Gabbana");
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-09-07 16:10:29 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-09-07 16:10:29 +08:00
|
|
|
var MAP = { '&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'"': '"',
|
|
|
|
"'": '''};
|
|
|
|
|
|
|
|
function convertHTML(str) {
|
|
|
|
return str.replace(/[&<>"']/g, function(c) {
|
|
|
|
return MAP[c];
|
|
|
|
});
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2020-09-07 16:10:29 +08:00
|
|
|
</section>
|