Regular Expressions: Best practice. Match Single Character with Multiple Possibilities

So, I just completed this challenge. On my first run through I did:

let vowelRegex = /[a|e|i|o|u]/gi;

Then I thought to see if this would work (both examples pass the challenge):

let vowelRegex = /[aeiou]/gi;

Is there a reason to use the OR operator like my first example when extracting single characters from a string? Would leaving the OR operator(s) out of it be considered best practice in this case since doing so achieves the same results using less code?

Thank you!

Your code so far

let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
let vowelRegex = /[aeiou]/ig; // Change this line
let result = quoteSample.match(vowelRegex); // Change this line

Challenge: Match Single Character with Multiple Possibilities

Link to the challenge:

No reason to use OR in the way you did at all, that’s exactly what a character class does on its own.

Edit: As pointed out below, the OR operator will be parsed as a character and not as an operator, so using it in the way OP did is actively harmful instead of just pointless.

by default characters listed inside square brackets are tested individually(only one match). So /a|e|i|o|u/ is similar to /[aeiou]/

/[a|e|i|o|u]/ is the same as /[a|eiou]/. | character will be check; it’s not OR here.

/[a|e|i|o|u]/ is not the same as /[aeiou]/

/a|e|i|o|u/ is the same as /[aeiou]/

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.