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 8923745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:13:09+00:00 2026-06-15T07:13:09+00:00

var target = $(.target-text), relation = [‘spun gold’, ‘gold’, ‘apple’], len = relation; for

  • 0
var target   = $(".target-text"),
    relation = ['spun gold', 'gold', 'apple'],
    len      = relation;

for (var i=0; i<len; i++) {

    var e = relation[i],
        re = new RegExp(e,"ig");

    if (e.length > 0) {
        target.html( target.html().replace(re, "<span class='hit'>$&</span>") );
    }

};

The searched characters are surrounded with a tag.

At this time, There is a problem.
The problem is that a result changes in the turn to search.

like a ['spun gold', 'gold', 'apple'] is ok,
but like a ['gold', 'spun gold', 'apple'] is ng.

If gold => <span class='hit'>gold</span> is performed first,
spun gold => <span class='hit'>spun gold</span> will go wrong.
(Since gold has changed to <span class='hit'>gold</span> by the 1st time.)

Is there better way?


EDIT:

As conditions, search is not only English.

  • 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-15T07:13:10+00:00Added an answer on June 15, 2026 at 7:13 am

    I think I see the problem: one replacement is messing up a previous one because they share a common substring.

    Perform a single replacement, by combining the terms in relation into one big “or” in the regexp:

    var target   = $('.target-text'),
        relation = ['spun gold', 'gold', 'apple'],
        re = new RegExp(relation.join('|'), 'ig');
    
    target.html(function (_, oldHtml) {
        return oldHtml.replace(re, "<span class='hit'>$&</span>");
    });
    

    N.B. you may need to quote the array elements before .join('|')ing them to prevent special characters from breaking the regexp. (Example: relation = ['foo', 'bar?']). Here’s how: Escape string for use in Javascript regex

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

Sidebar

Related Questions

JSFiddle URL: http://jsfiddle.net/sFe45/ Code: <html> <head> <title>Foo</title> <script type=text/javascript> var target; var anchor; function
Using this code var html='<div class=somediv></div>'; var target=document.getElementById('contentArea'); target.appendChild(html); I'm getting Uncaught Error: NOT_FOUND_ERR:
I have this in my JavaScript var identifier = event.target.id; it works on Chrome
$(#table_id).click(function(e) { var row = jQuery(e.target || e.srcElement).parent(); $(#table_id).bind('click', loaddata); name= row.attr(id); }); loaddata
I'm not sure how to target the element I want in this case: var
tar zcvf Test.tar.gz /var/trac/test /var/svn/test So far I have: <target name=archive depends=init> <tar compression=gzip
How do you define dynamic variables in a makefile target? For example: all: VAR
var ref1 = new Firebase(http://gamma.firebase.com/myuser/123,456); ref1.set(123,456); var on1 = ref1.on(value, function(snapshot) { console.log(snapshot.val()); });
var gethtml = $(#topmenu > li.l89 a).text().split(' ')[1].replaceWith('Any World'); The text is not changing
I have the following html <tr class=editorRow> <td class=emailRow> <input id=EmailSuccessList_0 name=EmailSuccessList[0] type=text value=asdf@lib.com>

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.