I made a code that select vowels and replace them with ""
function disemvowel(str) {
str = str.replace(/[aeiouAEIOU]/g, "");
return str;
}
console.log(disemvowel("This website is for losers LOL!"));
But I am not quite sure how this part of the code works =
/[aeiouAEIOU]/g why are the vowels inside [] and what the g does? as well as the //
Another question, how could I select both lower and upper case letter at once instead of right [aeiouAEIOU]?
>Solution :
/[aeiouAEIOU]/g
is short for
new RegExp( '[aeiouAEIOU]', 'g' )
It constructs a RegExp object which represents a regular expression and some flags. A regular expression defines a set of strings. String.prototype.replace can use this definition to identify the substrings to replace.
The regex pattern
[aeiouAEIOU]
defines the following set of strings: a, e, i, o, u, A, E, I, O, U.
The g flag tells String.prototype.replace to replaces all instances of those substrings (not just the first).
Note that /[aeiou]/ig would be sufficient. The i flag makes operations case-insensitive. (This may have a performance penalty.)