Counting vowels in javascript

前端 未结 5 1689
长情又很酷
长情又很酷 2021-01-24 12:41

I use this code to search and count vowels in the string,

a = \"run forest, run\";
a = a.split(\" \");
var syl = 0;
for (var i = 0; i < a.length - 1; i++) {
         


        
5条回答
  •  清酒与你
    2021-01-24 13:26

    You can use the .match to compare a string to a regular expression. g is global which will run through the entire string. i makes the string readable as upper and lower case.

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

提交回复
热议问题