Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8156427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:56:59+00:00 2026-06-06T16:56:59+00:00

I need to find text in html and highlight that html without corrupting html

  • 0

I need to find text in html and highlight that html without corrupting html itself which i being highlighted.

Text to Replace:

This is text 2. This is Text 3.

HTML:

This is text 1. <p>
This is <span>Text 2</span>. This <div>is</div> text 3.
</p> This is Text 4.

Desired Output:

This is text 1.<p>
<strong class="highlight">This is <span>Text 2</span>. This <div>is</div> text 3. </strong>
</p> This is Text 4.

EDIT: Sorry, if I was not able to explain properly.

I need to highlight a portion of html document (in php or javascript) if string i am searching matches to text in HTML.

But remember that the string i am searching my not be identical to search string, it may contain some extra HTML.

For example if i am searching for this string “This is text.“, it should be matched with “This is text.”, “<anyhtmltag>This</anyhtmltag> is text.”, “This <anyhtmltag>is</anyhtmltag> text.”, “This<anyhtmltag> is text</anyhtmltag>.” and so on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:57:01+00:00Added an answer on June 6, 2026 at 4:57 pm

    You need to be more specific, if you want to achieve this by either server-side (using PHP for example and returning to browser a HTML code already containing highlighted output) or client-side (using a jQuery for example to find and highlight something in HTML returned by server)?

    It seems to me, that you just asked a question, without doing nothing (like searching the net), as finding proper solution for jQuery (client-side) took me around TEN seconds! And three most important search results were on StackExchange and jQuery documentation itself.

    Find text using jQuery?

    Find text string using jQuery?

    jQuery .wrap() function description

    Here is an example in a very brief:

    <script>
         $('div:contains("This is <span>Text 2</span>. This <div>is</div> text 3")')wrap("<strong class="highlight"></strong>");
    </script>
    

    It generally finds, what you want to find and wraps it with what you want it to be wrapped with.

    This works, when the text you want to find is inside some div, that is why, I used $('div:contains. If you want to search whole page, you can use $('*:contains instead.

    This is example for jQuery and client-side highlighting. For PHP (server-side) version, do some little searching on either Google or StackOverflow and you’ll for sure find many examples.

    EDIT: As for your updated question. If you are using any textbox to put there, what you want to search, you can of course use something like this:

    <script>
        $("#mysearchbox").change(
        {
            $('div:contains($("#mysearchbox")').wrap("<strong class="highlight"></strong>");
        });
    </script>
    

    and define your search box somewhere else for example like this:

    <input id="mysearchbox"></input>
    

    This way, you’re attaching an onChange event to your search box, that will be fired anytime you type anything to it and that should find (and highlight) anything you entered.
    Note that this examples are from-hand (from memory). I don’t have access do jQuery from where I’m writing, so I can’t check, if there aren’t any mistakes, in what I’ve just wrote. You need to test it yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find picture location in HTML text based on regex. e.g. The
i have some text which I know and I need to find first tag
I need to find the text 'ifeq ($(Param1)' using grep. I try to assign
I have a block of text where I need yo find bits of text
I need to find a way to store 250 KB of plain text numbers
How to find text I am looking for in the following HTML (line breaks
Im trying to find a regular expression that matches this kind of URL: http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html
I need to send simple html-message with JavaMail. And when I tried to find
I need to download from FTP over 5000 files being .html and .php files.
I need to find basic WYSIWYG HTML editor component for C++Builder 5 to let

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.