Open In App

How to find sub-string between the two words using jQuery ?

Last Updated : 12 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string containing words and the task is to find substring between two given words using jQuery. There are two methods to solve this problem which are discussed below:

Using match() method: It searches a string for a match against any regular expression and if the match is found then it returns the match string as an array.

Syntax:

string.match( regexp )

Approach:

  • Get the string from the HTML element.
  • Provide the regular expression in the format like “starting_word(.*)ending_word”.
  • The substring between the starting_word and ending_word(exclusivly) will be extracted.

Example:




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to find substring between
        the two words using jQuery ?
    </title>
      
    <script src=
    </script>
</head>
  
<body style = "text-align:center;">
      
    <h1 style = "color:green;"
        GeeksforGeeks 
    </h1
      
    <h2>Given String</h2>
      
    <h4>
        GeeksforGeeks: A computer
        science portal for geeks
    </h4>
      
    <p>
        Click on the button to get the<br
        substring between "A" and "for"
    </p>
      
    <button onclick=myGeeks()>
        Click Here!
    </button>
      
    <p id="GFG"></p>
      
    <script>
        function myGeeks() {
            $(document).ready(function() {
                $("button").click(function() {
                    var str = "GeeksforGeeks: A computer"
                           + " science portal for geeks";
                      
                    var subStr = str.match("A(.*)for");
                      
                    document.getElementById('GFG').innerHTML
                                = subStr[1];
                });
            });
        }
    </script>
</body>
</html>


Output:

  • Before Click on button:
  • After Click on button:
  • Using split() method: It simply split a string into an array of substrings by a specified character or string and returns the new array.

    Syntax:

    string.split( separator, limit )

    Approach:

    • Get a string from the HTML element.
    • Split the string into substring on the basis of starting_word and ending_word.
    • The substring between the starting_word and ending_word(exclusivly) will be extracted.

    Example:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            How to find substring between
            the two words using jQuery ?
        </title>
          
        <script src=
        </script>
    </head>
      
    <body style = "text-align:center;">
          
        <h1 style = "color:green;"
            GeeksforGeeks 
        </h1
          
        <h2>Given String</h2>
          
        <h4>
            GeeksforGeeks: A computer
            science portal for geeks
        </h4>
          
        <p>
            Click on the button to get the<br
            substring between "A" and "for"
        </p>
          
        <button onclick=myGeeks()>
            Click Here!
        </button>
          
        <p id="GFG"></p>
          
        <script>
            function myGeeks() {
                $(document).ready(function() {
                    $("button").click(function() {
                        var str = "GeeksforGeeks: A computer"
                               + " science portal for geeks";
                          
                        var subStr = 
                            str.split('A').pop().split('for')[0];
                          
                        document.getElementById('GFG').innerHTML
                                    = subStr;
                    });
                });
            }
        </script>
    </body>
    </html>         

    
    

    Output:

  • Before Click on button:
  • After Click on button:


  • Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments

    Similar Reads