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
ascii - How to replace all special characters using javascript - Stack Overflow
I want to replace all special character in ASCII-%. Like this list: ? -> ? ! -> ! ... Till now i have replaced 2 special characters. But i doesnt know a simple method to replace all spec... More on stackoverflow.com
🌐 stackoverflow.com
string - How to replace special characters in JavaScript - Stack Overflow
How to replace the special characters like ")" in the string "Richardson & Cruddas (R & C), Jhon (J)" in javascript More on stackoverflow.com
🌐 stackoverflow.com
Find and replace all special characters in all json keys
I agree with the other posts, use a parser if possible. But if you are somehow limited to just RegEx and your json is as simple as the example you posted you can do this. Building on u/Kompaan86 ‘s approach, and matching only before the : [^A-Za-z0-9\n"](?=[^:\n]*:) Regex101 demo This can break in 100 ways, so please use a parser if at all possible. More on reddit.com
🌐 r/regex
6
3
May 5, 2022
🌐
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. Use /[^a-zA-Z0-9]/g to remove all special characters or create custom patterns for specific requirements.
🌐
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...
🌐
Metring
ricardometring.com › javascript-replace-special-characters
JavaScript: Replacing Special Characters - The Clean Way
The only addition, in this case, was to create 2 groups in the regex through ([ group 1 ]|[ group 2 ]) and add to group 2 the regular expression [^0-9a-zA-Z], which means: anything that's not (^) 0-9, a-z or A-Z, is also replaced. ... Another quite recurrent use case is the need to clear the accents and then replace special characters with some other one, e.g.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-special-characters-from-string
Remove special Characters from a String in JavaScript | bobbyhadz
The replace() method will return a new string that doesn't contain any special characters. ... Copied!const str = 'hello 123 !@#$%^WORLD?.'; const noSpecialCharacters = str.replace(/[^a-zA-Z0-9 ]/g, ''); console.log(noSpecialCharacters); // 👉️ 'hello 123 WORLD' ... The first argument we passed to the String.replace() method is a regular expression. We used the g (global) flag to match all occurrences of the regex in the string and not just the first occurrence.
🌐
Linux Hint
linuxhint.com › replace-all-special-characters-in-string-in-javascript
How to Replace All Special Characters in a String in ...
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › replace all special characters in javascript | example code
Replace all special characters in JavaScript | Example code
June 27, 2022 - 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). ... A simple example code uses a regular expression to replace them with the empty string. <!doctype html> <head> <script> var s = "img_realt@ime_tr~ading3$"; var res = s.replace(/[^\w\s]/gi, '') console.log(res) </script> </head> <body> </body> </html> ... Do comment if you have any doubts or suggestions on this JS replace code. Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
🌐
DigitalOcean
digitalocean.com › community › tutorials › replace-all-instances-of-a-string-in-javascript
How To Replace All Instances of a String in JavaScript | DigitalOcean
October 28, 2020 - const myMessage = 'this is the ... message to end all messages · To replace special characters like -/\^$*+?.()|[]{}), we’ll need to use a backslash to escape them....
🌐
DEV Community
dev.to › maafaishal › javascript-stringreplace-useful-cases-3963
JavaScript `string.replace()` useful cases - DEV Community
September 24, 2024 - Replace only whole words while ... Output: "This is a success word, test." Replace all occurrences of the whole word, use the global flag....
🌐
SmartWiki
wiki.smartsimple.com › wiki › Removing_Special_Characters
Removing Special Characters - SmartWiki
onkeyup=javascript:this.value=this.value.replace(/["]/g,'\''); 3. To restrict user to only enter Integers write this code in the HTML tag box: onkeyup="this.value=this.value.replace(/[\D]/g,'');" ... Retrieved from ‘https://wiki.smartsimple.com/index.php?title=Removing_Special_Characters...
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to remove special characters from a string in javascript
How to Remove Special Characters From a String in JavaScript
October 13, 2022 - To remove all special characters from a string, call the replace() method on the string, passing a whitelisting regex and an empty string as arguments, i.e., str.replace(/^a-zA-Z0-9 ]/g, '').
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to remove spaces and special characters from a string in javascript
How to Remove Spaces and Special Characters from a String in JavaScript
June 3, 2025 - Use str.replace(/\s/g, '') to remove all spaces. Alternatively, use str.split(' ').join('') for readability. The regex method str.replace(/[^a-zA-Z0-9]/g, '') is the most flexible and efficient for removing special characters while keeping ...
🌐
Sololearn
sololearn.com › en › Discuss › 2451169 › how-to-replace-all-the-special-characters-with-their-html-codes-in-javascript-
How to replace all the special characters with their HTML ...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › replace-special-characters-in-a-string-with-underscore-_-in-javascript
Replace special characters in a string with underscore (_) in JavaScript - GeeksforGeeks
July 11, 2025 - Special characters are non-alphanumeric ... ... The JavaScript replace() Method searches for a defined value (or pattern) within a string and returns a new string where the matched values are replaced by a specified value...
🌐
Maker's Aid
makersaid.com › home › remove special characters from a string in javascript
Remove Special Characters From a String in JavaScript - Maker's Aid
April 2, 2023 - To remove special characters from a string in JavaScript, we will use the String.replace() method with a global RegEx rule that looks for all matches of the characters we want removed, then replaces them with empty quotes ('').
🌐
CodePen
codepen.io › peterdillon › pen › OXXmjv
Replace all special characters jQuery
You can also link to another Pen ... from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.