How to remove vowels from a JS String?

1

I want to create a function in JS that removes vowels from a word. I tried with replace, regular expression, but I could not.

    
asked by anonymous 15.05.2018 / 00:35

2 answers

2

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 );
    
15.05.2018 / 00:58
1

Functional example with regular expressions:

const example = 'Olá, mundo!';

console.log(example.replace(/(a|e|i|o|u)/gi, ''));

Adding more characters

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
    
15.05.2018 / 00:39