10 examples of 'javascript html escape' in JavaScript

Every line of 'javascript html escape' 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
this disclaimer
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}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

27function htmlEscape( string ) {
28 return String( string ).
29 replace( /&/g, "&amp;" ).
30 replace( /"/g, "&quot;" ).
31 replace( /'/g, "&#39;" ).
32 replace( /</g, "&lt;" ).
33 replace( />/g, "&gt;" );
34}
6function escape(html, encode) {
7 return html
8 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
9 .replace(/</g, '&lt;')
10 .replace(/>/g, '&gt;')
11 .replace(/"/g, '&quot;')
12 .replace(/'/g, '&#39;');
13}
40function quoteEscape(s)
41{
42 s = s.replace(/"/g,'&quot;');
43 return s;
44}
121function escape (html, encode) {
122 return html
123 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
124 .replace(/</g, '&lt;')
125 .replace(/>/g, '&gt;')
126 .replace(/"/g, '&quot;')
127 .replace(/'/g, '&#39;')
128}
68function escape(html, encode) {
69 return html.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
70 .replace(/</g, '&lt;')
71 .replace(/>/g, '&gt;')
72 .replace(/"/g, '&quot;')
73 .replace(/'/g, '&#39;');
74}
7function htmlEscape(input: string) {
8 return input && input.replace(/&/g, "&amp;")
9 .replace(/</g, "&lt;")
10 .replace(/>/g, "&gt;")
11 .replace(/\n/g, "<br>");
12}
439function escape_html(str) {
440 // escape ampersands, angle brackets, tabs, and newlines
441 return str.replace(/\t/g, " ").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br />");
442}
109function htmlEscape(str) {
110 return str
111 .replace(/&/g, '&amp;')
112 .replace(/"/g, '&quot;')
113 .replace(/'/g, '&#39;')
114 .replace(/</g, '&lt;')
115 .replace(/>/g, '&gt;')
116 .replace(/\//g, '&#x2F;');
117}
46function escape(html){
47 return String(html)
48 .replace(/&(?!\w+;)/g, '&amp;')
49 .replace(/</g, '&lt;')
50 .replace(/>/g, '&gt;')
51 .replace(/"/g, '&quot;');
52}

Related snippets