xxxxxxxxxx
//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, '');
xxxxxxxxxx
var str = "Hello^# World/";
str.replace(/[^a-zA-Z ]/g, ""); // "Hello World"
xxxxxxxxxx
var desired = stringToReplace.replace(/[^\w\s]/gi, '')
//if you are using non english like arabic and other language
var outString = sourceString.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
xxxxxxxxxx
function removeSpecialCharacters(str) {
// Use a regular expression to match special characters and replace them with an empty string
return str.replace(/[^\w\s]/gi, "");
}
// Example usage
var inputString = "Hello @#%World!#$";
var result = removeSpecialCharacters(inputString);
console.log(result); // Output: "Hello World"
xxxxxxxxxx
const str = "abc's test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));
Run code snippet
xxxxxxxxxx
//123-456-7890
phone.replace(/[^0-9]/g, ''); //1234567890
//test_user
name.replace(/[^a-zA-Z]/g, " "); //test user
//test user@test.com
email.replace(/\s/g, ''); //testuser@test.com