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

As was mentioned in the comments it's easier to do this as a whitelist - replace the characters which aren't in your safelist.

The caret (^) character is the negation of the set [...], gi say global and case-insensitive (the latter is a bit redundant but I wanted to mention it) and the safelist in this example is digits, word characters, underscores (\w) and whitespace (\s).

Answer from annakata on Stack Overflow
🌐
CoreUI
coreui.io › answers › how-to-remove-special-characters-from-a-string-in-javascript
How to remove special characters from a string in JavaScript · CoreUI
May 20, 2026 - Use replace() with regular expressions to efficiently remove special characters from strings in JavaScript.
Discussions

Efficient way of replacing special characters - JavaScript - SitePoint Forums | Web Development & Design Community
Suppose I have a string like this: This is a fridge with 10 chocolates and ~5 icecream samples description to check if backward slash \ breaks anything and checking caret ^ symbol as well I want to replace those special characters with the following: \ to \1F ~ to \7E ^ to \5E I may find more ... More on sitepoint.com
🌐 sitepoint.com
0
April 9, 2024
How to remove a special characters in a given string by regex
how to remove a special characters in a given string by regex? “philippines nt., (Roosevelt Ave 3), St@a. philippines City, @U/SA” More on forum.uipath.com
🌐 forum.uipath.com
8
0
September 27, 2023
Remove special characters
Hello, I’m need to remove some special characters. I tried use replace function but I have some questions. I have an number like 123.456.789-01, and I need it to stay like this 12345678901 More on community.make.com
🌐 community.make.com
2
0
December 5, 2024
How to remove special characters and codes (using jQuery) from SharePoint rendered html? - SharePoint Stack Exchange
There may be unintended results from removing these without being more specific (so try in a test area, or better yet a development environment) ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Best of the Heap: First post of the... What it takes to be a player in the international AI... ... 2 How can I validate that any attachments file name does not have ANY special characters in JavaScript... More on sharepoint.stackexchange.com
🌐 sharepoint.stackexchange.com
February 26, 2018
🌐
freeCodeCamp
forum.freecodecamp.org › programming
Removing special characters from a string - Programming - The freeCodeCamp Forum
July 30, 2017 - Currently working on making a palindrome function. I have the gist of it. Just can’t get this last part. I’ve tried so many examples from so many stockoverflow posts and this still doesn’t work for me. Can someone help me figure out what’s wrong? var str = "abc's test#s"; str.replace(/[&\/\\#,+()$~%.'":*? {}]/g,'_'); console.log(str);
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence ...
🌐
SmartWiki
wiki.smartsimple.com › wiki › Removing_Special_Characters
Removing Special Characters - SmartWiki
Any individual character can be specified within the square brackets except double quote " which has special meaning. If you wish to include the backslash \ or closing square bracket ] you must precede them with a backslash (the first backslash tells the system to read the next character but not interpret it. See Example 2 below). ... Using onChange will replace the characters as soon as the user clicks away from current field, or hits Save. Using onKeyup will replace the characters as soon as they are typed. 1. To remove all characters that aren't part of the basic Latin alphabet and 0-9 (such as .
🌐
SitePoint
sitepoint.com › javascript
Efficient way of replacing special characters - JavaScript - SitePoint Forums | Web Development & Design Community
April 9, 2024 - Suppose I have a string like this: This is a fridge with 10 chocolates and ~5 icecream samples description to check if backward slash \ breaks anything and checking caret ^ symbol as well I want to replace those special characters with the following: \ to \1F ~ to \7E ^ to \5E I may find more to replace in future but at this point I just want to handle above such that after replacing it, the string looks like the following: This is a fridge with 10 horse and \7E5 goat samples descriptio...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › javascript-regex-how-to-replace-special-characters
JavaScript regex - How to replace special characters?
The replace() method with regex patterns provides a powerful way to remove or replace special characters in JavaScript strings.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-a-character-from-string-in-javascript
Remove a Character From String in JavaScript - GeeksforGeeks
February 25, 2026 - One of the most common methods to remove a character from a string is by using the replace() method. This method searches for a specified character or pattern in a string and replaces it with something else, which can be an empty string ("") ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions
Regular expressions - JavaScript | MDN
If you need to use any of the special characters literally (actually searching for a "*", for instance), you should escape it by putting a backslash in front of it.
🌐
UiPath Community
forum.uipath.com › help › studio
How to remove a special characters in a given string by regex - Studio - UiPath Community Forum
September 27, 2023 - how to remove a special characters in a given string by regex? “philippines nt., (Roosevelt Ave 3), St@a. philippines City, @U/SA”
🌐
Text-Utils
text-utils.com › remove special characters
Remove Special Characters - Online Regex Tools | Text-Utils.com
September 6, 2025 - Keep ASCII only: Remove all non-ASCII characters from the text. See the examples of usage below. ... Follow these steps to quickly delete unwanted symbols from the input. ... Have your text with unwanted characters ready.
🌐
Educative
educative.io › answers › how-to-remove-characters-from-a-string-in-javascript
How to remove characters from a string in JavaScript
By understanding these methods, ... Haven’t found what you were looking for? Contact Us · You can use the slice() method to remove the first 3 characters....
🌐
sebhastian
sebhastian.com › javascript-remove-special-characters-from-string
JavaScript Remove Special Characters From a String | sebhastian
October 25, 2023 - To remove special characters from a JavaScript string, you need to use the replace() method and define a regular expression to search for the special characters that need to be removed.
🌐
CodeSandbox
codesandbox.io › s › remove-special-characters-from-string-in-react-js-suhr8h
remove-special-characters-from-string-in-react-js - CodeSandbox
July 12, 2022 - remove-special-characters-from-string-in-react-js by infinitbility using react, react-dom, react-scripts
Published   Jul 12, 2022
Author   infinitbility
🌐
W3Schools
w3schools.com › js › js_strings.asp
JavaScript Strings
Templates are strings enclosed ... will be chopped to "We are the so-called ". To solve this problem, you can use an backslash escape character....
🌐
Make Community
community.make.com › questions
Remove special characters - Questions - Make Community
December 5, 2024 - Hello, I’m need to remove some special characters. I tried use replace function but I have some questions. I have an number like 123.456.789-01, and I need it to stay like this 12345678901
Top answer
1 of 2
1

This is one of the most annoying (and there are so many to choose from) things that SP does. We have a lot of lists that are used to display information. I found that preventing the zero width characters from getting into the list items in the first place was the best way to go about it.

There are probably other ways to attack this system wide, but I add the following code to all of our new/edit forms for lists that will be used to display content.

<script type="text/javascript">
function PreSaveAction(){
  // remove all the zero width spaces &#8203; from the fields.

  jQuery('.ms-rtestate-field div[contenteditable=true]').each(function(index,element)  {
       var exp = new RegExp(String.fromCharCode(8203),"g");
       var editor= jQuery(element);
       var txt = editor.html()
           txt = txt.replace(exp,'');
           txt = txt.replace(/&nbsp;/g,' ')
           txt = txt.replace(/ {2,}/g,' ');
       editor.html(txt);
  });

  return true;
}
</script>

This also gets rid of the non-breaking spaces and if somebody tries to line something up with a bunch of spaces. You can remove the last two txt.replace() lines if you don't want that.

We've been using this more than a year without problems in our 2013 on-prem site.

2 of 2
1

These are Unicode strings, the one you have there is space with 0 width (technically shouldn't add any width)... Microsoft's html generator adds these to control line breaks.

This will remove any non ascii characters. There may be unintended results from removing these without being more specific (so try in a test area, or better yet a development environment)

str.replace(/[^\x00-\x7F]/g, "");