string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_');

Alternatively, to change all characters except numbers and letters, try:

string = string.replace(/[^a-zA-Z0-9]/g,'_');
Answer from Niet the Dark Absol on Stack Overflow
🌐
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...
Discussions

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
Remove all special characters except space from a string using JavaScript - Stack Overflow
I want to remove all special characters except space from a string using JavaScript. For example, abc's test#s should output as abcs tests. More on stackoverflow.com
🌐 stackoverflow.com
How do I replace special characters with regex in javascript? - Stack Overflow
So by putting characters in [^...], you can decide which characters should be allowed and all others replaced. ... The December 2024 Community Asks Sprint has been moved to March 2025 (and... Stack Overflow Jobs is expanding to more countries · 5 Regular expression - replace special characters, except dot · 0 javascript ... More on stackoverflow.com
🌐 stackoverflow.com
Removing special characters from a string
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"; ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
13
0
July 30, 2017
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › article › javascript-regex-how-to-replace-special-characters
JavaScript regex - How to replace special characters?
March 15, 2026 - For this we use replace() method. The JavaScript built-in method string.replace() can be used to replace a portion of a supplied string with another string or a regular expression.
🌐
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...
🌐
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.
🌐
DEV Community
dev.to › maafaishal › javascript-stringreplace-useful-cases-3963
JavaScript `string.replace()` useful cases - DEV Community
September 24, 2024 - To replace characters that aren't ... = str.replace(/\d{3}/g, "***"); // Output: "Contact: ***-***-***0" You can also use Unicode escape sequences to replace special characters....
Find elsewhere
🌐
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...
🌐
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 - To replace special characters like -/\^$*+?.()|[]{}), we’ll need to use a backslash to escape them.
🌐
Medium
nick3499.medium.com › javascript-replace-special-characters-in-string-from-a-hash-table-replace-regex-8efb1c856799
JavaScript: Replace Special Characters in String from a Hash Table | by nick3499 | Medium
May 31, 2018 - Those values will be used to replace matching special characters: ... The arrow function expression calls replace() to compare each character with the regex pattern. If a match is found, the matching character is replaced with the hash table value.
🌐
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, ''). The replace() method will return a new string that ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript - MDN Web Docs
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 ...
🌐
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);
🌐
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 - The regex method str.replace(/[^a-zA-Z0-9]/g, '') is the most flexible and efficient for removing special characters while keeping alphanumeric ones. Regex is often the fastest for complex patterns.