JavaScript replace special characters code example

Example 1: regex special characters javascript

var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g

Example 2: js remove special characters

var desired = stringToReplace.replace(/[^\w\s]/gi, '')

Example 3: javascript html special characters

return mystring.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");

Example 4: c# string remove special characters

public static string RemoveSpecialCharacters(this string str) {
   StringBuilder sb = new StringBuilder();
   foreach (char c in str) {
      if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_') {
         sb.Append(c);
      }
   }
   return sb.ToString();
}