Skip to content
Related Articles

Related Articles

Improve Article

How to count string occurrence in string using JavaScript?

  • Last Updated : 22 Apr, 2019

In JavaScript, we can count the string occurrence in a string by counting the number of times the string present in the string. JavaScript provides a function match(), which is used to generate all the occurrences of a string in an array.
By counting the array size which will return the number of times the sub-string present in a string.
Script to find the number of occurrences in a string:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example-1: Counting occurrence of Geeks in “GeeksforGeeks” using “match()” function.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            count string occurrence in a string
        </title>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;"
            GeeksForGeeks 
        </h1>
        <h3>
          count string occurrence in a string 
      </h3>
      
        <button onClick="gfg()">
            count
        </button>
        <p id="rk">
      
        </p>
      
        <!-- Script to count string 
             occurrence in a string  -->
        <script>
            function gfg() {
                var r = "Geeks For Geeks ";
                document.getElementById("rk").innerHTML =
                  (r.match(/Geeks/g)).length;
      
            }
        </script>
    </body>
      
    </html>

    Output:

    • Before clicking on the Button:

    • After clicking on the Button:



    The ‘g’ in the function specifies global which is used to search for an entire string rather than stop by finding the first occurrence.

  • Another Example to count the number of times the string appears in a string.
    Example-2: Counting occurrence of for in “GeeksforGeeks” using Loops.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            count string occurrence in a string
        </title>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;"
            GeeksForGeeks 
        </h1>
        <h3>
          count string occurrence in a string 
      </h3>
      
        <button onClick="gfg()">
            count
        </button>
        <p id="rk">
      
        </p>
      
        <!-- Script to count 
            string occurrence in a string  -->
        <script>
            function gfg() {
                var s = "Geeks for Geeks";
                var f = "for";
                var i = 0,
                    n = 0,
                    j = 0;
      
                while (true) {
                    j = s.indexOf(f, j);
                    if (j >= 0) {
                        n++;
                        j++;
                    } else
                        break;
                }
                document.getElementById(
                  "rk").innerHTML = n;
            }
        </script>
    </body>
      
    </html>

    Output:

    • Before clicking on the Button :

    • After clicking on the Button:

  • Example-3: Counting occurrence of Geeks in “GeeksforGeeks” using split function.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            count string occurrence in a string
        </title>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;"
            GeeksForGeeks 
        </h1>
        <h3>
          count string occurrence in a string 
      </h3>
      
        <button onClick="gfg()">
            count
        </button>
        <p id="rk">
      
        </p>
      
        <!-- Script to count string 
             occurrence in a string  -->
        <script>
            function gfg() {
                var s = "Geeks for Geeks";
                var f = "Geeks";
                var r = s.split(f).length - 1;
                document.getElementById("rk").innerHTML = r;
            }
        </script>
    </body>
      
    </html>

    Output:

    • Before clicking on the Button:

    • After clicking on the Button:

  • Example-4:Counting occurrence of Geeks in “GeeksforGeeks” using indexof().




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            count string occurrence in a string
        </title>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;"
            GeeksForGeeks 
        </h1>
        <h3>
          count string occurrence in a string
      </h3>
      
        <button onClick="gfg()">
            count
        </button>
        <p id="rk">
      
        </p>
      
        <!-- Script to count 
             string occurrence in a string  -->
        <script>
            function gfg() {
                var s = "Geeks for Geeks";
                var f = "Geeks";
                var r = s.indexOf(f);
                var c = 0;
                while (r != -1) {
                    c++;
                    r = s.indexOf(f, r + 1);
                }
                document.getElementById("rk").innerHTML = c;
            }
        </script>
    </body>
      
    </html>

    Output:

    • Before clicking on the Button :

    • After clicking on the Button:




    My Personal Notes arrow_drop_up