How to get nth occurrence of a string in JavaScript ?

02 Jun, 2020
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 occurrence 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 occurrence = 3;                  el_up.innerHTML = "Click on the button to get the index of "                + occurrence + "rd occurrence 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, occurrence);        }    </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 occurrence = 3;                  el_up.innerHTML = "Click on the button to get the index of "                + occurrence + "rd occurrence 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, occurrence);        }    </script> </body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
nidhi_biet
Akanksha_Rai
javascript-string
JavaScript
Web Technologies
Web technologies Questions
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
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?