How to get nth occurrence of a string in JavaScript ?
The task is to get the nth occurrence of a substring in a string with the help of JavaScript.
Approach 1:
- First, split the string into sub-strings using split() method by passing index also.
- Again join the substrings on passed substring using join() method.
- Returns the index of nth occurence of string.
Example:In this example, split() and join() method are used to get the index of substring.
<!DOCTYPE HTML> <html> <head> <title> How to get nth occurrence of a string in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var string = "Geeks gfg Geeks Geek Geeks gfg"; var searchString = 'Geeks'; var occurance = 3; el_up.innerHTML = "Click on the button to get the index of " + occurance + "rd occurance of a '" + searchString + "' in a <br>'" + string + "'."; function getPos(str, subStr, i) { return str.split(subStr, i).join(subStr).length; } function GFG_Fun() { el_down.innerHTML = getPos(string, searchString, occurance); } </script> </body> </html> |
Output:
- Before clicking on the button:

- After clicking on the button:

Approach 2: Go through each substring one by one and return the index of the last substring. This approach uses indexOf() method to return the index of nth occurrence of the string.
Example:
<!DOCTYPE HTML> <html> <head> <title> How to get nth occurrence of a string in JavaScript ? </title> </head> <body style = "text-align:center;" id = "body"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var string = "Geeks gfg Geeks Geek Geeks gfg"; var searchString = 'Geeks'; var occurance = 3; el_up.innerHTML = "Click on the button to get the index of " + occurance + "rd occurance of a '" + searchString + "' in a <br>'" + string + "'."; function getIndex(str, substr, ind) { var Len = str.length, i = -1; while(ind-- && i++ < Len) { i = str.indexOf(substr, i); if (i < 0) break; } return i; } function GFG_Fun() { el_down.innerHTML = getIndex(string, searchString, occurance); } </script> </body> </html> |
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- How to count string occurrence in string using 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.fromCharCode()
- JavaScript to keep only first N characters in a string
- JavaScript | String indexOf()
- JavaScript | String substr()
- How to get value of a string after last slash in JavaScript?
- JavaScript | String charAt()
- JavaScript | string.normalize()
- JavaScript | String includes()
- JavaScript | string.replace()
- JavaScript | String endsWith()
- Reverse a String in JavaScript
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.



