I'm supposed to write a function that takes a character (i.e. a string of length 1) and returns true if it is a vowel, false otherwise. I came up with two functions, but don't know which one is better performing and which way I should prefer. The one with RegEx is way simpler but I am unsure whether I should try to avoid using RegEx or not?
Without RegEx:
function isVowel(char) {
if (char.length == 1) {
var vowels = new Array("a", "e", "i", "o", "u");
var isVowel = false;
for (e in vowels) {
if (vowels[e] == char) {
isVowel = true;
}
}
return isVowel;
}
}
With RegEx:
function isVowelRegEx(char) {
if (char.length == 1) {
return /[aeiou]/.test(char);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…