Use the global flag:
var name = name.replace(/[^a-zA-Z ]/g, "");
^
If you don't want to remove numbers, add it to the class:
var name = name.replace(/[^a-zA-Z0-9 ]/g, "");
Answer from Jerry on Stack OverflowUse the global flag:
var name = name.replace(/[^a-zA-Z ]/g, "");
^
If you don't want to remove numbers, add it to the class:
var name = name.replace(/[^a-zA-Z0-9 ]/g, "");
To remove the special characters, try
var name = name.replace(/[!@#$%^&*]/g, "");
Hi, I am trying to remove non-alphabetical characters from the following string.
let string = "Anne, I vote more cars race Rome-to-Vienna"
I can get the result I am looking for with the following...
let newString = string.toLowerCase().split('').filter(el => el !== ' ' && el !== '-' && el !== ',').join('')
However I would prefer to use a much shorter piece of code using regex. I have gone over a few regex examples and tutorials but it still kind of confused me. Does anybody know the simplest way to remove the unwanted characters from the above string?
Thanks!!!