jQuery

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

In this tutorial, we are going to see how to find sub-string between the two words using jQuery. You can use JavaScript’s match() method to extract a substring between two words.

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

How to find sub-string between the two words using jQuery
<!DOCTYPE html>
<html>
<head>
<title>Find sub-string between the two words</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
            var subStr = str.match("ipsum(.*)adipiscing");
            alert(subStr[1]);
        });
    });
</script>
</head>
<body>
    <p>Click on the following button to get the substring between the word "ipsum" and "adipiscing", in the sentence "Lorem ipsum dolor sit amet, consectetur adipiscing elit."</p>
    <button type="button">Get the substring</button>
</body>
</html>
Result

Click on the following button to get the substring between the word "ipsum" and "adipiscing", in the sentence "Lorem ipsum dolor sit amet, consectetur adipiscing elit."

[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *