How to find sub-string between the two words using jQuery ?
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:


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:


Recommended Posts:
- How to calculate the number of words in a string using JQuery?
- jQuery | find() with Examples
- How to find element with specific ID using jQuery ?
- jQuery | jQuery.support Property
- jQuery | jQuery.fx.interval Property with example
- How to find the parent class name with known class in jQuery ?
- jQuery | jQuery.fx.off Property
- jQuery | jquery Property
- JavaScript | Reserved Words
- How to make horizontal line with words in the middle using CSS?
- How to get a substring between two strings in PHP?
- Python | Program to crawl a web page and get most frequent words
- PHP to check substring in a string
- Difference between substr() and substring() in JavaScript
- How to check a string is entirely made up of the same substring 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.



