如何检查JavaScript中的元音?

时间:2021-07-09 07:23:23

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?

我应该编写一个函数,它取一个字符(即长度为1的字符串),如果是元音,则返回true,否则返回false。我想到了两个功能,但不知道哪个功能更好,哪个更好。使用RegEx的方法比较简单,但我不确定是否应该尝试避免使用RegEx ?

My attempt 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);
  }
}

8 个解决方案

#1


35  

benchmark

基准

I think you can safely say a for loop is faster.

我想你可以有把握地说,for循环比较快。

I do admit that a regexp looks cleaner in terms of code. If it's a real bottleneck then use a for loop, otherwise stick with the regular expression for reasons of "elegance"

我承认regexp在代码方面看起来更干净。如果它是一个真正的瓶颈,那么使用for循环,否则出于“优雅”的原因,请使用正则表达式

If you want to go for simplicity then just use

如果你想追求简单,那就用吧。

function isVowel(c) {
    return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1
}

#2


11  

Lost of answers here, speed is irrelevant for such small functions unless you are calling them a few hundred thousand times in a short period of time. For me, a regular expression is best, but keep it in a closure so you don't build it every time:

失去了答案,速度对于如此小的函数是无关的,除非你在短时间内调用它们几十万次。对我来说,一个正则表达式是最好的,但是要保持一个闭包,这样你就不会每次都构建它:

Simple version:

简单的版本:

function vowelTest(s) {
  return (/^[aeiou]$/i).test(s);
}

More efficient version:

更高效的版本:

var vowelTest = (function() {
  var re = /^[aeiou]$/i;
  return function(s) {
    return re.test(s);
  }
})();

Returns true if s is a single vowel (upper or lower case) and false for everything else.

如果s是一个单元音(大写或小写),则返回true,而对于其他所有内容则返回false。

#3


6  

cycles, arrays, regexp... for what? It can be much quicker :)

周期、数组、regexp……为了什么?它可以更快:

function isVowel(char)
{
    return char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u' || false;
}

#4


2  

Personally, I would define it this way:

我个人是这样定义的:

function isVowel( chr ){ return 'aeiou'.indexOf( chr[0].toLowerCase() ) !== -1 }

You could also use ['a','e','i','o','u'] and skip the length test, but then you are creating an array each time you call the function. (There are ways of mimicking this via closures, but those are a bit obscure to read)

您还可以使用['a','e','i','o','u']并跳过长度测试,但是每次调用函数时都要创建一个数组。(有一些方法可以通过闭包来模拟这种情况,但这些方法读起来有点晦涩难懂)

#5


1  

function isVowel(char)
{
  if (char.length == 1)
  {
    var vowels = "aeiou";
    var isVowel = vowels.indexOf(char) >= 0 ? true : false;

    return isVowel;
  }
}

Basically it checks for the index of the character in the string of vowels. If it is a consonant, and not in the string, indexOf will return -1.

它主要检查元音字符串中字符的索引。如果它是一个辅音,而不是在字符串中,indexOf将返回-1。

#6


1  

This is a rough RegExp function I would have come up with (it's untested)

这是一个粗糙的RegExp函数,我将会给出它(未经过测试)

function isVowel(char) {
    return /^[aeiou]$/.test(char.toLowerCase());
}

Which means, if (char.length == 1 && 'aeiou' is contained in char.toLowerCase()) then return true.

这意味着,如果(char。长度= 1 & 'aeiou'包含在char.toLowerCase()中,然后返回true。

#7


0  

function findVowels(str) {
  return (str.match(/[aeiou]/ig)||[]);
}

findVowels('abracadabra'); // 'aaaaa'

Basically it returns all the vowels in a given string.

基本上它返回给定字符串中的所有元音。

#8


-1  

  //function to find vowel
const vowel = (str)=>{
  //these are vowels we want to check for
  const check = ['a','e','i','o','u'];
  //keep track of vowels
  var count = 0;
  for(let char of str.toLowerCase())
  {
    //check if each character in string is in vowel array
    if(check.includes(char)) count++;
  }
  return count;
}

console.log(vowel("hello there"));

#1


35  

benchmark

基准

I think you can safely say a for loop is faster.

我想你可以有把握地说,for循环比较快。

I do admit that a regexp looks cleaner in terms of code. If it's a real bottleneck then use a for loop, otherwise stick with the regular expression for reasons of "elegance"

我承认regexp在代码方面看起来更干净。如果它是一个真正的瓶颈,那么使用for循环,否则出于“优雅”的原因,请使用正则表达式

If you want to go for simplicity then just use

如果你想追求简单,那就用吧。

function isVowel(c) {
    return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1
}

#2


11  

Lost of answers here, speed is irrelevant for such small functions unless you are calling them a few hundred thousand times in a short period of time. For me, a regular expression is best, but keep it in a closure so you don't build it every time:

失去了答案,速度对于如此小的函数是无关的,除非你在短时间内调用它们几十万次。对我来说,一个正则表达式是最好的,但是要保持一个闭包,这样你就不会每次都构建它:

Simple version:

简单的版本:

function vowelTest(s) {
  return (/^[aeiou]$/i).test(s);
}

More efficient version:

更高效的版本:

var vowelTest = (function() {
  var re = /^[aeiou]$/i;
  return function(s) {
    return re.test(s);
  }
})();

Returns true if s is a single vowel (upper or lower case) and false for everything else.

如果s是一个单元音(大写或小写),则返回true,而对于其他所有内容则返回false。

#3


6  

cycles, arrays, regexp... for what? It can be much quicker :)

周期、数组、regexp……为了什么?它可以更快:

function isVowel(char)
{
    return char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u' || false;
}

#4


2  

Personally, I would define it this way:

我个人是这样定义的:

function isVowel( chr ){ return 'aeiou'.indexOf( chr[0].toLowerCase() ) !== -1 }

You could also use ['a','e','i','o','u'] and skip the length test, but then you are creating an array each time you call the function. (There are ways of mimicking this via closures, but those are a bit obscure to read)

您还可以使用['a','e','i','o','u']并跳过长度测试,但是每次调用函数时都要创建一个数组。(有一些方法可以通过闭包来模拟这种情况,但这些方法读起来有点晦涩难懂)

#5


1  

function isVowel(char)
{
  if (char.length == 1)
  {
    var vowels = "aeiou";
    var isVowel = vowels.indexOf(char) >= 0 ? true : false;

    return isVowel;
  }
}

Basically it checks for the index of the character in the string of vowels. If it is a consonant, and not in the string, indexOf will return -1.

它主要检查元音字符串中字符的索引。如果它是一个辅音,而不是在字符串中,indexOf将返回-1。

#6


1  

This is a rough RegExp function I would have come up with (it's untested)

这是一个粗糙的RegExp函数,我将会给出它(未经过测试)

function isVowel(char) {
    return /^[aeiou]$/.test(char.toLowerCase());
}

Which means, if (char.length == 1 && 'aeiou' is contained in char.toLowerCase()) then return true.

这意味着,如果(char。长度= 1 & 'aeiou'包含在char.toLowerCase()中,然后返回true。

#7


0  

function findVowels(str) {
  return (str.match(/[aeiou]/ig)||[]);
}

findVowels('abracadabra'); // 'aaaaa'

Basically it returns all the vowels in a given string.

基本上它返回给定字符串中的所有元音。

#8


-1  

  //function to find vowel
const vowel = (str)=>{
  //these are vowels we want to check for
  const check = ['a','e','i','o','u'];
  //keep track of vowels
  var count = 0;
  for(let char of str.toLowerCase())
  {
    //check if each character in string is in vowel array
    if(check.includes(char)) count++;
  }
  return count;
}

console.log(vowel("hello there"));