Count the vowels of an input?

后端 未结 6 1977
小鲜肉
小鲜肉 2021-01-27 10:25

Hi I am trying to count the vowels of the input of the form in my HTML using javascript currently with no success.

Here is my HTML


         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-27 10:51

    Looking at the other answers, I think this is The Simplest Solution

    One line of code and case insensitive. It splits the string at vowels and returns the array length. Why make it more complicated with loops and switch statements?

    update: I really like freefaller's solution using match(), which is equal in simplicity. The solution here has a slight edge in that it has wider browser support. Would be interesting to know if there's any speed advantage between the methods.

    Solution:

    var vowels = text.split(/[aeiou]/gi).length - 1;
    

    Run code snippet to test:

    
    
    
      
      

    Vowel Counter Demo

    Count:0

提交回复
热议问题