-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1410-html-entity-parser.js
37 lines (35 loc) · 1.18 KB
/
1410-html-entity-parser.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
31
32
33
34
35
36
37
/**
* 1410. HTML Entity Parser
* https://leetcode.com/problems/html-entity-parser/
* Difficulty: Medium
*
* HTML entity parser is the parser that takes HTML code as input and replace all the
* entities of the special characters by the characters itself.
*
* The special characters and their entities for HTML are:
* - Quotation Mark: the entity is " and symbol character is ".
* - Single Quote Mark: the entity is ' and symbol character is '.
* - Ampersand: the entity is & and symbol character is &.
* - Greater Than Sign: the entity is > and symbol character is >.
* - Less Than Sign: the entity is < and symbol character is <.
* - Slash: the entity is ⁄ and symbol character is /.
*
* Given the input text string to the HTML parser, you have to implement the entity parser.
*
* Return the text after replacing the entities by the special characters.
*/
/**
* @param {string} text
* @return {string}
*/
var entityParser = function(text) {
const map = {
'"': '"',
''': '\'',
'&': '&',
'>': '>',
'<': '<',
'⁄': '/'
};
return text.replace(new RegExp(Object.keys(map).join('|'), 'g'), m => map[m]);
};