Description:

In this article, I will explain how to find occurrence of string using Jquery.  

To find occurrence of string use match() method

Syntax:

string.match(regularexpression)

match() used to search string with the regular expression and it will return only the first match in the string

If no match is found, it will return null value.

Here, I am going to search the string with the click event.

If the word matches with the target string, some action will happen based on your requirement.

Example Program:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#txtSearchWord').bind('keyup', function () {
                var wordtofind = "occurrence";
                var textboxval = $('#txtSearchWord').val();
                var existsword = textboxval.match(wordtofind);
                if (existsword !== null)
                    $.each('p', function (i) {
                        var Wholetexts = $('p').text();
                        Wholetexts = Wholetexts.replace(textboxval, function (e) {
                            return "<span style='background-color: gray;'>" + e + "</span>";
                        });
                        $('p').html(Wholetexts);
                        alert("Word Occured");
                    });

            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            Find the word 'occurance'
        </h3>
        <p>
            Sample content for Find occurrence of string</p>
        <asp:TextBox ID="txtSearchWord" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>
  
In the above example, I am searching a word ‘occurrence’ with the match() method. If the word matches with the target string, it will return alert method. If no match found means, it will return null value.


Find Matched Text with Jquery , Find Matched string using match function, Find occurrence of a word using Jquery, Highlight search terms using Jquery, Find Matching words using Jquery, Search word using match function in jquery, Search string using Jquery

0 comments:

Post a Comment

 
Top