How to find substring between the two words using jQuery

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the JavaScript match() method

You can use the JavaScript match() method to extract substring between two words.

The following example will show you how to extract substring between the two words using the match() method and a simple regular expression.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="UTF-8">
  • <title>jQuery Get Substring Between Two Words</title>
  • <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
  • <script type="text/javascript">
  •     $(document).ready(function(){
  •         $("button").click(function(){
  •             var myStr = "The quick brown fox jumps over the lazy dog.";
  •             var subStr = myStr.match("quick(.*)lazy");
  •             alert(subStr[1]);
  •         });
  •     });
  • </script>
  • </head>
  • <body>
  •     <p>Click the following button to get the substring between the word "quick" and "lazy" from the string "The quick brown fox jumps over the lazy dog."</p>
  •     <button type="button">Get Substring</button>
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.