Counting vowels in javascript

前端 未结 5 1741
长情又很酷
长情又很酷 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:15

    Try this:

    var syl = ("|"+a+"|").split(/[aeiou]/i).length-1;
    

    The | ensures there are no edge cases, such as having a vowel at the start or end of the string.

提交回复
热议问题