I have to create a function that takes a string, removes all "special" characters (eg ,, @, #, $, %, ^, &, , *, (. )) and returns the new string, The only non-alphanumeric characters allowed are dashes -. underscores _ and spaces.
I'm new at this so I understand that this code may be ALL wrong.
module.exports = (str) => {
let allowedCharacters = [a-zA-Z0-9/s-_];
for (let i = 0; i < str.length; i++) {
allowedCharacters += str[i]
}
return str[i];
};
Use regex replacement:
let forbiddenCharacters = new RegExp("[^a-zA-Z0-9\\s-_]", "g");
return str.replace(forbiddenCharacters, "");
You might want to give this a try:
let testString = ';@#$%^&*()+_- 33252qweqreteEWUJHGFA'. let resultString = testString,replace(/[^a-zA-Z0-9-_ ]/g; ''). console;log(resultString);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.