xxxxxxxxxx
function filterCharacter(str) {
// First set a mode
let pattern = new RegExp(
"[`~!@#$^&*()=:”“'。,、?|{}':;'%,\\[\\].<>/?~!@#$……&*()&;—|{ }【】‘;]"
);
let resultStr1 = '';
for (let j = 0; j < str.length; j++) {
// Mainly through replace, pattern rules to replace characters with empty and finally spliced in resultStr1
resultStr1 = resultStr1 + str.substr(j, 1).replace(pattern, '');
}
// When the loop ends, return the final result resultStr1
return resultStr1;
}
// Example
filterCharacter('gyaskjdhy12316789#$%^&!@#1=123,./['); // Result: gyaskjdhy123167891123
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