I want to create a function in JS that removes vowels from a word. I tried with replace, regular expression, but I could not.
I want to create a function in JS that removes vowels from a word. I tried with replace, regular expression, but I could not.
You can use this regex that will remove even accented vowels:
/[aeiouà-ú]/gi
Flags:
g -> global. Busca por todas as ocorrências.
i -> case insensitive. Não faz distinção entre maiúsculas e minúsculas.
See:
function removeVogaisString( remove ){
return remove.replace(/[aeiouà-ú]/gi,'');
}
var resultado = removeVogaisString( "OláÁéôãõ, mundo!" );
console.log( resultado );
Functional example with regular expressions:
const example = 'Olá, mundo!';
console.log(example.replace(/(a|e|i|o|u)/gi, ''));
If you want to add more characters to be removed, just add more to the side of |u
.
For example, if you want to remove the letter z
also, just change the expression of:
/(a|e|i|o|u)/gi
To:
/(a|e|i|o|u|z)/gi