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 Top answer 1 of 3
276
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_');
Alternatively, to change all characters except numbers and letters, try:
string = string.replace(/[^a-zA-Z0-9]/g,'_');
2 of 3
10
string = string.replace(/[\W_]/g, "_");
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...
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
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
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
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
04:48
Remove All Special Characters from String — JavaScript Regex ...
04:05
How to remove special characters from a given string using JavaScript.
01:45
How do I remove special characters in a string in JavaScript? - ...
13:42
Remove special Characters from a String in JavaScript (Article ...
07:35
How to Find and Replace Special Characters in Microsoft Word - YouTube
02:17
Replace All Special Characters in JavaScript - YouTube
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.
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.
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.
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 ...
Top answer 1 of 13
570
You should use the string replace function, with a single regex. Assuming by special characters, you mean anything that's not letter, here is a solution:
const str = "abc's test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
2 of 13
211
You can do it specifying the characters you want to remove:
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
Alternatively, to change all characters except numbers and letters, try:
string = string.replace(/[^a-zA-Z0-9]/g, '');
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);
Top answer 1 of 5
7
Sure is!
Running this in the Firebug console
"®ü".replace(/[®ü]/g,"replaced")
returned
"replacedreplaced"
You can also do
"®ü".replace(/[\xAE\xFC]/g,"Wohoo! ");
which returns
"Wohoo! Wohoo! "
A good hex symbol lookup page can be found at http://www.ascii.cl/htmlcodes.htm
Example
running this jQuery on this page
$(".post-text").text().replace(/®/g," ******** ")
returns
" is it possible to translate special characters like ******** , ü etc with javascript
String replace function? Use this syntax... string.replace(/\xCC/g/,''); Where 'CC' is
the hex character code for the char you are wanting to replace. In this example I am
replacing with empty string ''. yes, and is as simple as can be: ' ******** '.replace('
******** ','anything'); Sure is! Running this in the Firebug console " ******** ü".
replace(/[ ******** ü]/g,"replaced") returned replacedreplaced "
2 of 5
2
yes, and is as simple as can be:
'®'.replace('®','anything');