• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

how to search and highlight txt in html file

I want to search any string in html file and as soon as it found i want to highlight with some color
it can have any other formatting tags in between it should remain as it is. i am trying this in c#.net
0
chand_c
Asked:
chand_c
1 Solution
 
Tony McCreathTechnical SEO ConsultantCommented:
This might work
htmlText.Replace(text, "<span style=\"background-color: yellow;\">"+text+"</span>");

Open in new window

0
 
masterpassCommented:
Try the code below

ASPX
-------------
<asp:Panel ID="SearchPanel" runat="server" DefaultButton="SearchButton">
    <div style="float: right;">
        <asp:TextBox ID="SearchWordTextBox" runat="server" Width="120" SkinID="TextBox_FormInput-SingleLine"></asp:TextBox>
        <asp:Button ID="SearchButton" runat="server" Text="Search" SkinID="Button_FormCommand" />
    </div>
    <div style="clear: both;">
    </div>
</asp:Panel>

JAVASCRIPT
------------
<script type="text/javascript" language="javascript">
    var OriginalBody;
    var OriginalBodyCounter = 0;
    var FinderCounter;

    function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) {
        // the highlightStartTag and highlightEndTag parameters are optional
        if ((!highlightStartTag) || (!highlightEndTag)) {
            highlightStartTag = "<font style='background-color:Red;'>";
            highlightEndTag = "</font>";
        }

        // find all occurences of the search term in the given text,
        // and add some "highlight" tags to them (we're not using a
        // regular expression search, because we want to filter out
        // matches that occur within HTML tags and script blocks, so
        // we have to do a little extra validation)
        var newText = "";
        var i = -1;
        var lcSearchTerm = searchTerm.toLowerCase();
        var lcBodyText = bodyText.toLowerCase();

        while (bodyText.length > 0) {
            i = lcBodyText.indexOf(lcSearchTerm, i + 1);
            if (i < 0) {
                newText += bodyText;
                bodyText = "";

            } else {
                // skip anything inside an HTML tag
                if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
                    // skip anything inside a <script> block
                    if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
                        newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
                        bodyText = bodyText.substr(i + searchTerm.length);
                        lcBodyText = bodyText.toLowerCase();
                        i = -1;
                        FinderCounter++;
                    }
                }
            }
        }

        return newText;
    }


    /*
    * This is sort of a wrapper function to the doHighlight function.
    * It takes the searchText that you pass, optionally splits it into
    * separate words, and transforms the text on the current web page.
    * Only the "searchText" parameter is required; all other parameters
    * are optional and can be omitted.
    */
    function highlightSearchTerms(ctrl, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag) {
        // if the treatAsPhrase parameter is true, then we should search for 
        // the entire phrase that was entered; otherwise, we will split the
        // search string so that each word is searched for and highlighted
        // individually
        var searchText = document.getElementById('<%= SearchWordTextBox.ClientID %>').value;
        if (searchText != "") {
            if (treatAsPhrase) {
                searchArray = [searchText];
            } else {
                searchArray = searchText.split(" ");
            }
            FinderCounter = 0;
            if (!document.body || typeof (document.body.innerHTML) == "undefined") {
                if (warnOnFailure) {
                    alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
                }
                return false;
            }
            if (OriginalBodyCounter < 1)
                OriginalBody = document.getElementById(ctrl).innerHTML;
            var bodyText = OriginalBody;
            for (var i = 0; i < searchArray.length; i++) {
                bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
            }
            OriginalBodyCounter++;
            document.getElementById(ctrl).innerHTML = bodyText;
            if (OriginalBody == bodyText)
                alert('No matches found');
            else
                alert(FinderCounter + " match(es) found");
        }
        else if (OriginalBodyCounter >= 1) {
            document.getElementById(ctrl).innerHTML = OriginalBody;
        }

        return false;
    }

</script>

Open in new window

0
 
chand_cAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now