- switch (string.charCodeAt(index)) {
- case 60: // <
- escape = '<';
- break;
- case 62: // >
- escape = '>';
- break;
- default:
- continue;
- }
module
ES6
- // List of the characters to escape
- const chars = {
- ">": ">",
- "<": "<",
- };
- // RegExp from the `chars` object
- const re = new RegExp(Object.keys(chars).join("|"), "g");
- // Return the escaped string
- const escapeHtml = (str = "") => String(str).replace(re, match => chars[match]);
- console.dir( escapeHtml('<div>'));