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:


Recommended Posts:
- How to get nth occurrence of a string in JavaScript ?
- JavaScript | Difference between String.slice and String.substring
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Insert a string at position X of another string
- JavaScript | string.normalize()
- JavaScript | String concat()
- JavaScript to keep only first N characters in a string
- How to get the last character of a string in JavaScript?
- JavaScript | String() Function
- Reverse a String in JavaScript
- JavaScript | String charAt()
- JavaScript | string.replace()
- JavaScript | String.fromCharCode()
- JavaScript | String substr()
- JavaScript | string.codePointAt()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



