I want to output a string's vowels in order, so I decided to use RegEx to do it.
However, when I put the expression in different position in (if and else if), the output is different for the same expression. Could anyone explain?
function ordered_vowel_word(str) {
if(str.match(/[aeiou]{1,}/g) !== ""){
var arr = str.match(/[aeiou]{1,}/g);
console.log(arr);
}
else
console.log(str);
}
ordered_vowel_word("bcg");
ordered_vowel_word("eebbscao");
/* The Output */
ordered_vowel_word("bcg");
==> null
ordered_vowel_word("eebbscao");
==> ["ee", "ao"]
But if I restructure the expression,
function ordered_vowel_word(str) {
if(str.match(/[^aeiou]/) !== "")
console.log(str);
else if(str.match(/[aeiou]{1,}/g) !== ""){
var arr = str.match(/[aeiou]{1,}/g);
console.log(arr);
}
}
The output will be
ordered_vowel_word("bcg");
==> bgv
ordered_vowel_word("eebbscao");
==> eebbscao