Counting number of vowels in a string with JavaScript

前端 未结 18 1609
旧巷少年郎
旧巷少年郎 2020-12-08 23:53

I\'m using basic JavaScript to count the number of vowels in a string. The below code works but I would like to have it cleaned up a bit. Would using .includes()

18条回答
  •  隐瞒了意图╮
    2020-12-09 00:19

    Just use this function [for ES5] :

    function countVowels(str){
        return (str.match(/[aeiou]/gi) == null) ? 0 : str.match(/[aeiou]/gi).length;        
    }
    

    Will work like a charm

提交回复
热议问题