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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:10:06+00:00 2026-06-05T15:10:06+00:00

I am trying to create a text highlight option for my website. But I

  • 0

I am trying to create a text highlight option for my website. But I want exact word match instead of fuzzy word match, The code that I have matches all kind of instances and it has some case sensitivity issues. If we take the Jfiddle example, I only want to add the word cancer, case sensitivity should nt be an issue. and ignore fuzzy matches, like cancerous and bycanceraous(I know there is not a word like that, but could not think of any for the example). I have the jfiddle link
http://jsfiddle.net/ehzPQ/6/

HTML:

<div id="entity">cancer</div>
<div id="article">
  This kind of insurance is meant to supplement health insurance for cancerous-care costs. But generally you're better off putting your money toward comprehensive health policies. The I just repeat health insurance, because it sounds so good! health insurance, health insurance, I can never grow tired of it... Cancer is seriously a dangerouse disease. Test case : bycanceraous
</div>​

CSS:

.highlight {
    background-color: yellow
}​

Javascript:

$(document).ready(function(){
  var $test = $('#article');
  var entityText = $('#entity').html();
  var entityRegularExpression = new RegExp(entityText,"g");
  var highlight = '<span class="highlight">' + entityText + '</span>';
  $test.html($test.html().replace(entityRegularExpression, highlight));
});
  • 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-05T15:10:08+00:00Added an answer on June 5, 2026 at 3:10 pm

    You need to utilize Regular Expression’s Word Boundaries.

    Change the following line:

    var entityRegularExpression = new RegExp(entityText, "g");
    

    To this:

    var entityRegularExpression = new RegExp("\\b" + entityText + "\\b", "gi");
    

    Here’s the updated jsfiddle.
    Note: I updated the article text to contain some instances of the word so you could see it work.

    You can also take it a step further and have the case insensitive matches retain their original case, by utilizing Regular Expression’s Callbacks. Check out this jsfiddle for the code and example.

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

Sidebar

Related Questions

I'm trying to create text in html, that once clicked, the the value of
I am trying to create full text index but I am getting error which
I am trying to create a simple Javascript text editor that only makes paragraph
I am trying to create a text box that grows every time a letter
I'm trying to create a textarea that will highlight some keywords as the user
I'm trying to create a text widget that includes an entry form on top
I am trying to create a simple appplication that can read from a text
I'm trying to create some text headings that have a line out to the
I am trying to create a text file using VB.Net with UTF8 encoding, without
I am trying to create a text-based game in Java similar to Zork and

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.