the efficient way to remove all special characters from a string javascript code example

Example 1: remove special characters from string javascript

var str = "Hello^# World/";
str.replace(/[^a-zA-Z ]/g, ""); // "Hello World"

Example 2: js remove special characters

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

Example 3: 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();
}