通过 split() 函数
var charCount = function(s, ch) {
return s.split(ch).length - 1;
}
str = 'hello world';
ch = 'o';
console.log(charCount(str, ch)); // 2
var charCount = function(s, ch) {
return s.split(ch).length - 1;
}
str = 'hello world';
ch = 'o';
console.log(charCount(str, ch)); // 2