How to count string occurrence in string using JavaScript?
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:
<!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.
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 = "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.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:







Please Login to comment...