文字列からある文字が何個含まれているか数える

splitを使って

String.prototype.countS = function(str) {
    return this.split(str).length-1;
}

matchを使って

String.prototype.countM = function(str) {
    return this.match(RegExp(str,'g')).length;
}

replaceをつかって

String.prototype.countR = function(str) {
    var n=0,regex = RegExp(str,'g');
    this.replace(regex,function() {
        n++;
    });
    return n;
}

実行例

js> a='hoge fuga hige mage'
hoge fuga hige mage
js> a.countS('g')
4
js> a.countM('ge')
3
js> a.countR('h')
2