javascript count how many times a word appears in a string code example

Example 1: javascript count occurrences in 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

Example 2: js string includes count

/** Function that count occurrences of a substring in a string;
 * @param {String} string               The string
 * @param {String} subString            The sub string to search for
 * @param {Boolean} [allowOverlapping]  Optional. (Default:false)
 *
 * @author Vitim.us https://gist.github.com/victornpb/7736865
 * @see Unit Test https://jsfiddle.net/Victornpb/5axuh96u/
 * @see http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string/7924240#7924240
 */
function occurrences(string, subString, allowOverlapping) {

    string += "";
    subString += "";
    if (subString.length <= 0) return (string.length + 1);

    var n = 0,
        pos = 0,
        step = allowOverlapping ? 1 : subString.length;

    while (true) {
        pos = string.indexOf(subString, pos);
        if (pos >= 0) {
            ++n;
            pos += step;
        } else break;
    }
    return n;
}

Example 3: Count Total Amount Of Specific Word In a String JavaScript

Here are two methods (Scroll down please here!) to find the total number of occurrence match
words in the string.

The first function allows you to give a query as input.
The second one uses the .match function of JavaScript.

Both introduced methods are resistant for any chars and 
independent of splitter and separator like " " or ",".

str1 is your query

str1 = "fake";  

str2 is the whole string:

var inputString = "fakefakefakegg fake 00f0 221 Hello wo fake misinfo
fakeddfakefake , wo  431,,asd misinfo misinfo co wo fake sosis bandari
mikhori?, fake fake fake ";

Method 1 : use .indexOf or .search function of JavaScript 
(advantage you can give input)


function CountTotalAmountOfSpecificWordInaString(str1, str2)
{
    let next = 0;
    let findedword = 0;
        do {
            var n = str2.indexOf(str1, next);
            findedword = findedword +1;
            next = n + str1.length;
            }while (n>=0);
     console.log("total finded word :" , findedword - 1 );
     return findedword;
   }
Method 2 : use .match function of JavaScript:



/**
 * @return {number}
 *  you have to put fake as query manually in this solution!!! disadvantage
 */

function CountTotalAmountOfMachedWordInaString(str2) {
    let machedWord = 0;
    machedWord = str2.match(/fake/g).length; 
    console.log("total finded mached :" , machedWord);
    return machedWord;
}
call the functions (Inputs):

CountTotalAmountOfSpecificWordInaString("fake" , "fake fakefakegg fake 00f0 221 Hello wo fake rld fakefakefake , wo lklsak dalkkfakelasd co wo fake , fake fake fake" );
CountTotalAmountOfMachedWordInaString("sosis bandarie fake  khiyarshour sosis , droud bar fake to sosis3");