Javascript String stringCount(text, sub)

Description

Javascript String stringCount(text, sub)



String.prototype.stringCount = function (text, sub) {

    text = text.toLowerCase();//from w  ww  .j a v a 2  s  .co  m
    sub = sub.toLowerCase();
    var indexOfSub = text.indexOf(sub);
    var count = 0;

    while (indexOfSub > -1) {

        count++;
        indexOfSub = text.indexOf(sub, indexOfSub + 1);
    }

    return count;
}



PreviousNext

Related