How to count string occurrence in string using JavaScript?

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:
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.
<!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:
<!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:
<!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_upSave
<!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:
JavaScript-Misc
javascript-string
Picked
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Roadmap to Learn JavaScript For Beginners
How to get character array from string in JavaScript?
How do you run JavaScript script through the Terminal?
JavaScript | console.log() with Examples