1 function htmlEscape(text) {
2 return text.replace(/[<>"&]/g, function (match, pos, originalText) {
3 switch (match) {
4 case "<":
5 return "<";
6 case ">":
7 return ">";
8 case "&":
9 return "&";
10 case "\"":
11 return """;
12 }
13 });
14 }
15 console.log(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
2 return text.replace(/[<>"&]/g, function (match, pos, originalText) {
3 switch (match) {
4 case "<":
5 return "<";
6 case ">":
7 return ">";
8 case "&":
9 return "&";
10 case "\"":
11 return """;
12 }
13 });
14 }
15 console.log(htmlEscape("<p class=\"greeting\">Hello world!</p>"));