10 examples of 'js escape quotes' in JavaScript

Every line of 'js escape quotes' 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
43function escapeSingleQuote (string) {
44 if (typeof string !== 'string') {
45 return string
46 }
47 return string.replace(/'/g, '\\\'')
48}
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}
9function escapeSingleQuotes (value) {
10 return value.replace(/'/g, ''')
11}
40function quoteEscape(s)
41{
42 s = s.replace(/"/g,'"');
43 return s;
44}
30function escape(val: string, quoteValues: boolean) {
31 if (isObject(val)) {
32 val = val.valueOf();
33 }
34
35 val = String(val);
36
37 if (quoteValues && nonAlphaNumRE.test(val)) {
38 val = `"${val.replace(allDoubleQuoteRE, '""')}"`;
39 }
40
41 return val;
42}
1export function escapeQuotes(input: string) {
2 // double escape quotes so that they are not unescaped completely in the template string
3 return input.replace(/"/g, '\\"');
4}
9export function escapeQuotes(value, defaultValue = '') {
10 return value ? value.replace(/(^|[^\\])"/g, '$1\\\"') : defaultValue;
11}
7function safeEscapeQuotes(str) {
8 return str.replace(/\\([\s\S])|(')/g,"\\$1$2").replace(/\\([\s\S])|(")/g,"\\$1$2"); // escape only if not escaped already
9}
34function escapeString(val, forbidQualified) {
35 if (Array.isArray(val)) {
36 var sql = '';
37
38 for (var i = 0; i < val.length; i++) {
39 sql += (i === 0 ? '' : ', ') + escapeString(val[i], forbidQualified);
40 }
41
42 return sql;
43 }
44
45 if (forbidQualified) {
46 return '' + val.replace(/`/g, '``') + '';
47 }
48
49 return '' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '';
50}
51export function escapeDoubleQuotes(string) {
52 return string.replace(/\"/g, '\\$&');
53}

Related snippets