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
chand_cAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chand_cAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.