7 examples of 'escapehtml javascript' in JavaScript

Every line of 'escapehtml javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
19export function escapeHtml(string) {
20 var str = '' + string;
21 var match = matchHtmlRegExp.exec(str);
22
23 if (!match) {
24 return str;
25 }
26
27 var escape;
28 var html = '';
29 var index = 0;
30 var lastIndex = 0;
31
32 for (index = match.index; index < str.length; index++) {
33 switch (str.charCodeAt(index)) {
34 case 34: // "
35 escape = '"';
36 break;
37 case 38: // &
38 escape = '&';
39 break;
40 case 39: // '
41 escape = ''';
42 break;
43 case 60: // <
44 escape = '<';
45 break;
46 case 62: // >
47 escape = '>';
48 break;
49 default:
50 continue;
51 }
52
53 if (lastIndex !== index) {
54 html += str.substring(lastIndex, index);
55 }
56
57 lastIndex = index + 1;
58 html += escape;
59 }
60
61 return lastIndex !== index
62 ? html + str.substring(lastIndex, index)
63 : html;
64}
78function escapeHTML( string, multiline ) {
79 return String( string ).replace( /&(?!\w+;)|[<>"']/g, function ( s ) {
80 return escapeMap[ s ] || s;
81 });
82}
23function escapeHTML (unescaped) {
24 return unescaped.replace(htmlRegexp, (match) => htmlSymbols[match])
25}
98function escapeHTML(html) {
99 escape.textContent = html;
100 return escape.innerHTML;
101}
94export function escapeJavascript(text: string): string {
95 let result = text;
96 let escape;
97 let start = 0;
98 let i = 0;
99 for (; i < text.length; ++i) {
100 switch (text.charCodeAt(i)) {
101 case 47: // /
102 escape = "\\u002F";
103 break;
104 case 60: // <
105 escape = "\\u003C";
106 break;
107 case 62: // >
108 escape = "\\u003E";
109 break;
110 case 8232:
111 escape = "\\u2028";
112 break;
113 case 8233:
114 escape = "\\u2029";
115 break;
116 default:
117 continue;
118 }
119 if (i > start) {
120 escape = text.slice(start, i) + escape;
121 }
122 result = (start > 0) ? result + escape : escape;
123 start = i + 1;
124 }
125 if (start !== 0 && i !== start) {
126 return result + text.slice(start, i);
127 }
128 return result;
129}
33function escapeHTML (str) {
34 return new window.Option(str).innerHTML
35}
51function escapeHTML(string) {
52 return String(string).replace(/&(?!\w+;)|[<>"']/g, function (s) {
53 return escapeMap[s] || s;
54 });
55}

Related snippets