JavaScript menghitung instance dalam string

function countOccurences(string, word) {
   return string.split(word).length - 1;
}
var text="We went down to the stall, then down to the river."; 
var count=countOccurences(text,"down"); // 2
Grepper