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

Related snippets