Someone explain this code to me?

Someone explain this code to me?
0

#1

I solved a simple function of removing all the vowels out of a sentence and then found this solution trying to simplify it. Is this what people call regex? Why did the person use “j” instead of anything else. I know typically people use “i” or “j”, but why “j” in this case, is it simply just arbitrary? So, anything in the forward slashes and brackets will be replaced with what is after the comma (an empty space). What do the “g” and “i” stand for? Global…and?

Thank you all so much.

function disemvowel(str) {
  let j = str.replace(/[aeiou]/gi, '');
  return j;
}

#2

yes this is a regular expression solution. You will study it later in the javascript section if you haven’t yet.

as for j, that’s just a variable name.
you can use what you like but try to use something meaningful…


#3

The “g” and “i” are referred to as flags. The flags are:

Flag Description
g Global search.
i Case-insensitive search.
m Multi-line search.
u unicode; treat a pattern as a sequence of unicode code points
y Perform a “sticky” search that matches starting at the current position in the target string. See sticky

The most common are “g” and “i”. Normally the replace function would only replace the first match. By using the “g” flag, it will replace all matches. The “i” makes it so that regardless of case it would still match. For example, if I had the the following code:

var str = "AAaaBBbbCCcc";
var str_noB = str.replace(/[b]/g, '*');

str_noB would now look like “AAaaBB**CCcc”. However, if I add the “i” flag to the regular expression, then str_noB would look like “AAaa****CCcc”.


#4

Thank you as always.