-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-convert-html-entities.js
30 lines (28 loc) · 1.22 KB
/
11-convert-html-entities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Convert HTML Entities
Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
- convertHTML("Dolce & Gabbana") should return the string Dolce & Gabbana.
- convertHTML("Hamburgers < Pizza < Tacos") should return the string Hamburgers < Pizza < Tacos.
- convertHTML("Sixty > twelve") should return the string Sixty > twelve.
- convertHTML('Stuff in "quotation marks"') should return the string Stuff in "quotation marks".
- convertHTML("Schindler's List") should return the string Schindler's List.
- convertHTML("<>") should return the string <>.
- convertHTML("abc") should return the string abc.
*/
function convertHTML(str) {
const htmlEntities = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
};
return str.replace(/[&<>"']/g, match => htmlEntities[match]);
}
console.log(convertHTML("Dolce & Gabbana"));
console.log(convertHTML("Hamburgers < Pizza < Tacos"));
console.log(convertHTML("Sixty > twelve"));
console.log(convertHTML('Stuff in "quotation marks"'));
console.log(convertHTML("Schindler's List"));
console.log(convertHTML("<>"));
console.log(convertHTML("abc"));