Remove all non alphabetic chars from string in Javascript
To remove all non-alphanumeric characters from a string, call the replace() method, passing it a regular expression that matches all non-alphanumeric characters as the first parameter and an empty string as the second. The replace method returns a new string with all matches replaced.
remove non letters from a string javascript
// remove non-letters from a string
function lettersOnly(str) {
return str.replace(/[^a-zA-Z]/g,"");
//or return str.match(/[a-z]/gi).join('');
//or return str.replace(/[^a-z]/gi,"");
}
console.log(lettersOnly("R!=:~0o0./c&}9k`60=y")); //"Rocky"
console.log(lettersOnly("^,]%4B|@56a![0{2m>b1&4i4")); //"Bambi"
console.log(lettersOnly("^U)6$22>8p).")); //"Up"
const str = 'A!@#b$%^c&*(';
const replaced = str.replace(/[^a-z0-9]/gi, '');
console.log(replaced); // 👉️ Abc