1function count(str, find) {
2 return (str.split(find)).length - 1;
3}
4
5count("Good", "o"); // 2
1var temp = "This is a string.";
2var count = (temp.match(/is/g) || []).length;
3console.log(count);
4
5Output: 2
6
7Explaination : The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches 'is' twice.
1function countOccurences(string, word) {
2 return string.split(word).length - 1;
3}
4var text="We went down to the stall, then down to the river.";
5var count=countOccurences(text,"down"); // 2
1/** Function that count occurrences of a substring in a string;
2 * @param {String} string The string
3 * @param {String} subString The sub string to search for
4 * @param {Boolean} [allowOverlapping] Optional. (Default:false)
5 *
6 * @author Vitim.us https://gist.github.com/victornpb/7736865
7 * @see Unit Test https://jsfiddle.net/Victornpb/5axuh96u/
8 * @see http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string/7924240#7924240
9 */
10function occurrences(string, subString, allowOverlapping) {
11
12 string += "";
13 subString += "";
14 if (subString.length <= 0) return (string.length + 1);
15
16 var n = 0,
17 pos = 0,
18 step = allowOverlapping ? 1 : subString.length;
19
20 while (true) {
21 pos = string.indexOf(subString, pos);
22 if (pos >= 0) {
23 ++n;
24 pos += step;
25 } else break;
26 }
27 return n;
28}
1Here are two methods (Scroll down please here!) to find the total number of occurrence match
2words in the string.
3
4The first function allows you to give a query as input.
5The second one uses the .match function of JavaScript.
6
7Both introduced methods are resistant for any chars and
8independent of splitter and separator like " " or ",".
9
10str1 is your query
11
12str1 = "fake";
13
14str2 is the whole string:
15
16var inputString = "fakefakefakegg fake 00f0 221 Hello wo fake misinfo
17fakeddfakefake , wo 431,,asd misinfo misinfo co wo fake sosis bandari
18mikhori?, fake fake fake ";
19
20Method 1 : use .indexOf or .search function of JavaScript
21(advantage you can give input)
22
23
24function CountTotalAmountOfSpecificWordInaString(str1, str2)
25{
26 let next = 0;
27 let findedword = 0;
28 do {
29 var n = str2.indexOf(str1, next);
30 findedword = findedword +1;
31 next = n + str1.length;
32 }while (n>=0);
33 console.log("total finded word :" , findedword - 1 );
34 return findedword;
35 }
36Method 2 : use .match function of JavaScript:
37
38
39
40/**
41 * @return {number}
42 * you have to put fake as query manually in this solution!!! disadvantage
43 */
44
45function CountTotalAmountOfMachedWordInaString(str2) {
46 let machedWord = 0;
47 machedWord = str2.match(/fake/g).length;
48 console.log("total finded mached :" , machedWord);
49 return machedWord;
50}
51call the functions (Inputs):
52
53CountTotalAmountOfSpecificWordInaString("fake" , "fake fakefakegg fake 00f0 221 Hello wo fake rld fakefakefake , wo lklsak dalkkfakelasd co wo fake , fake fake fake" );
54CountTotalAmountOfMachedWordInaString("sosis bandarie fake khiyarshour sosis , droud bar fake to sosis3");
55