Remove vowels from a string in Javascript


Hello I was doing a function that removes all vowels from a string, it works fine for me when I do it for a letter .. like this:

function eliminarVocal(str) {
    var resultado = str.replace(/a/g, '');

    return resultado

If I do multiple .replace (/ vowel / g, '') it would work for me but I was thinking something more optimal, to eliminate all the letters specified in an arrangement or something like that and it doesn't work for me .. How could I do it .. Thanks


You were very close, although you do not need an arrangement, with a simple regular expression you can solve it, be careful only with those that do not have accents for this example.

 function eliminarVocales(str) { let resultado = str.replace(/[aeiou]/g, '') return resultado } var text = "Hola Prueba de Replace y/o Javascript"; const a = eliminarVocales(text); console.log(a);

This simple regular expression [aeiou] set to global, combined with the replace function, finds all the characters indicated in the brackets and replaces them with an empty character.

To be used with all vowels including accented ones, capital letters;

 function eliminarVocales(str) { let resultado = str.replace(/[aáAÁeéEÉiíIÍoOóÓuúUÚ]/g, '') return resultado } var text = "Holá PruebA de REplacé y/o JÁvascrípt"; const a = eliminarVocales(text); console.log(a);
Scroll to Top