Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How the selection of the letter work in function replace /[aeiou/g]?

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]?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Read this.

/[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.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading