3 examples of 'find special characters in string' in JavaScript

Every line of 'find special characters in string' 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
1export function escapeSpecialChars(str) {
2 return str
3 .replace(/&/g, "&")
4 .replace(/</g, "&lt;")
5 .replace(/>/g, "&gt;")
6 .replace(/"/g, "&quot;")
7 .replace(/'/g, "&#039;");
8}
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

140function SpecialCharDecode(s) {
141 var tagsToReplace = {
142 '&lt;': '<',
143 '&gt;': '>',
144 '&#91;': '[',
145 '&#93;': ']',
146 '&#34;': '"'
147 };
148
149 return s.replace(/&lt;|&gt;|&#91;|&#93;|&#34;/g, function (tag) {
150 return tagsToReplace[tag] || tag;
151 });
152 //return s.replace(/\[/g, "&#91;").replace(/\]/g, "&#93;").replace(/\"/g, "'");
153 //return s.replace(/[\u00A0-\u9999<>\&]/gim, function(i) { return '&#'+i.charCodeAt(0)+';'; });
154}
11function filterControlCharacters(string) {
12 if(string) {
13 return string.replace(/[\x00-\x1F\x7F-\x9F]/g, '');
14 }
15}

Related snippets