Skip to content
Related Articles

Related Articles

Improve Article

How to get nth occurrence of a string in JavaScript ?

  • Last Updated : 02 Jun, 2020
Geek Week

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:
    nth-occurrence-01
  • After clicking on the button:
    nth-occurrence-02

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:
    nth-occurrence-03
  • After clicking on the button:
    nth-occurrence-04

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :