2021-06-15 07:49:18 +00:00
|
|
|
---
|
|
|
|
id: a6b0bb188d873cb2c8729495
|
2021-07-21 15:23:20 +00:00
|
|
|
title: Converter entidades HTML
|
2021-06-15 07:49:18 +00:00
|
|
|
challengeType: 5
|
|
|
|
forumTopicId: 16007
|
|
|
|
dashedName: convert-html-entities
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-07-16 05:33:16 +00:00
|
|
|
Converta os caracteres `&`, `<`, `>`, `"` (aspas duplas) e `'` (aspas simples), em uma string para suas entidades HTML correspondentes.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-07-16 05:33:16 +00:00
|
|
|
`convertHTML("Dolce & Gabbana")` deve retorna a string `Dolce & Gabbana`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.match(convertHTML('Dolce & Gabbana'), /Dolce & Gabbana/);
|
|
|
|
```
|
|
|
|
|
2021-07-16 05:33:16 +00:00
|
|
|
`convertHTML("Hamburgers < Pizza < Tacos")` deve retornar a string `Hamburgers < Pizza < Tacos`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.match(
|
|
|
|
convertHTML('Hamburgers < Pizza < Tacos'),
|
|
|
|
/Hamburgers < Pizza < Tacos/
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-07-16 05:33:16 +00:00
|
|
|
`convertHTML("Sixty > twelve")` deve retornar a string `Sixty > twelve`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.match(convertHTML('Sixty > twelve'), /Sixty > twelve/);
|
|
|
|
```
|
|
|
|
|
2021-07-16 05:33:16 +00:00
|
|
|
`convertHTML('Stuff in "quotation marks"')` deve retornar a string `Stuff in "quotation marks"`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.match(
|
|
|
|
convertHTML('Stuff in "quotation marks"'),
|
|
|
|
/Stuff in "quotation marks"/
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-07-16 05:33:16 +00:00
|
|
|
`convertHTML("Schindler's List")` deve retornar a string `Schindler's List`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.match(convertHTML("Schindler's List"), /Schindler's List/);
|
|
|
|
```
|
|
|
|
|
2021-07-16 05:33:16 +00:00
|
|
|
`convertHTML("<>")` deve retornar a string `<>`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.match(convertHTML('<>'), /<>/);
|
|
|
|
```
|
|
|
|
|
2021-07-16 05:33:16 +00:00
|
|
|
`convertHTML("abc")` deve retornar a string `abc`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.strictEqual(convertHTML('abc'), 'abc');
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function convertHTML(str) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
convertHTML("Dolce & Gabbana");
|
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
|
|
|
var MAP = { '&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'"': '"',
|
|
|
|
"'": '''};
|
|
|
|
|
|
|
|
function convertHTML(str) {
|
|
|
|
return str.replace(/[&<>"']/g, function(c) {
|
|
|
|
return MAP[c];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
```
|