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

  • SEARCH
  • Home
  • 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 1049889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:41:13+00:00 2026-05-16T16:41:13+00:00

Having a small problem for a quick Search and Highlight script that I’m working

  • 0

Having a small problem for a quick “Search and Highlight” script that I’m working on. I’m using regular expressions because I’d like to do the searching all on client side, after the document has loaded. My search/highlight function goes like this:

function highlight(word, colour, container) {
    var regex = new RegExp("(>[^<]*?)(" + word + ")", "ig");
    var replace = "$1<span name='searchTerm' style='background-color: " + colour + "'>$2</span>";

    if (regex.exec(container.innerHTML)) {
        container.innerHTML = container.innerHTML.replace(regex, replace);
        return true;
    }
    return false;
}

word is the word to search for, colour is the colour to highlight it and container is the element to search in.

Consider an element that contained this:

<ul>
    <li>Set the setting to the correct setting.</li>
</ul>

Say I passed the word “set” to the highlight function. In it’s current state, it only finds the first instance of set due to lazy repitition.

So what if I change the regex to this:

var regex = new RegExp("(>[^<]*?)?(" + word + ")", "ig");

This now works great, it highlights all instances of the string “set”. But if I pass the search word “li” then it will replace the text inside the tags!

Is there a quick fix for this regular expression to get the behaviour I want? I need it to replace all instances of the search string but not those found as part of a tag. I’d like to keep it client-side using regex.

Thanks!

  • 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-05-16T16:41:14+00:00Added an answer on May 16, 2026 at 4:41 pm

    You shouldn’t be using regex to parse HTML. Walk the DOM tree properly and do a search and replace on pure text.

    By the way there’s a jQuery plugin that does what you want; you could use it or look at it to get an idea on how to do it:
    http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html

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

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Look at the following post I created describing how you… May 16, 2026 at 6:30 pm
  • Editorial Team
    Editorial Team added an answer A service provider interface (or SPI) for short is a… May 16, 2026 at 6:30 pm
  • Editorial Team
    Editorial Team added an answer If the goal is to simply group like sequences together,… May 16, 2026 at 6:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

i'm having a small problem here. I'm using a simple rule to redirect all
I am currently writing a quick script in Ruby that goes through all the
Im having a small problem, I'm trying to create an inline editing system with
I'm having a small problem with my setup. I'm quite new to the Django
Edit 1: Uninstalled & Reinstalled Edit 2: Same problem. Seriously? Yes. I am having
I've been trying to write a small library using Thor to help assist me
I am having problems with the google maps API V3. It seems that every
It seems that a lot of small business people have a need for some
I have a design problem I'd like to solve. I have an interface, lets
I work as a lone developer in a very small company. My work is

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.