Remove special characters (like !, >, ?, ., # etc.,) from a string using JavaScript:
var temp = new String('This is a te!!!!st st>ring... So??? What...');
document.write(temp + '<br>');
temp = temp.replace(/[^a-zA-Z 0-9]+/g,'');
document.write(temp + '<br>');
jsFiddle
Edit:
If you don't want to remove dot(.) from string:
temp = temp.replace(/[^a-zA-Z 0-9.]+/g,'');
Answer from Naveed on Stack Overflowjavascript replacing special characters - Stack Overflow
javascript - Replace string with special characters for empty string - Stack Overflow
Efficient way of replacing special characters - JavaScript - SitePoint Forums | Web Development & Design Community
Remove all special characters except space from a string using JavaScript - Stack Overflow
How to remove specific characters from a string in JavaScript?
How to remove space from a string in JavaScript?
How to give space in a string in JavaScript?
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 "
yes, and is as simple as can be:
'®'.replace('®','anything');
Use following code:
url.toString().replace(/&roadmap=1/g, '');
This is going to be regarding the type of url.
whatever url is, it's not a string. It has to be a string for the method replace to work on it.
You can use console.log(typeof url); to find out what it is.
You can learn more about types here: JavaScript data types and data structures
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
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, '');
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).
Note that if you still want to exclude a set, including things like slashes and special characters you can do the following:
var outString = sourceString.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
take special note that in order to also include the "minus" character, you need to escape it with a backslash like the latter group. if you don't it will also select 0-9 which is probably undesired.