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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:00:33+00:00 2026-05-13T02:00:33+00:00

I am implementing a website search and am trying to highlight the words the

  • 0

I am implementing a website search and am trying to highlight the words the user searched for using the below code:

data = Regex.Replace(data, Model.SearchCriteria, "<strong>" + Model.SearchCriteria + "</strong>", RegexOptions.IgnoreCase);

However if data is “I went North towards Canada” and the user has searched for “north” the results will show “I went north towards Canada” with north highlighted however the actual data has been replaced incorrectly slightly.

How can I keep the returned data in tact whilst higlighting what the user searched for?

  • 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-13T02:00:33+00:00Added an answer on May 13, 2026 at 2:00 am

    In this case you need to use a substitution pattern to put the original text into the replaced string vs. the explicit search criteria

    data = Regex.Replace(
      "("+Model.SearchCriteria+")",
      "<strong>$1</strong>",
      RegexOptions.IgnoreCase);
    

    Putting parens around the search criteria places it into an unnamed group. You can then reference this group by index in the replacement string by using $1. This will then use the original matched text.

    Info on substitution strings in Regex.Replace

    • http://msdn.microsoft.com/en-us/library/ewy2t5e0.aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing caching for my website which is using Zend Framework. I look
implementing publishActivity in PHP using the REST API using this code: $activity = array(
I am trying to integrate NHibernate.Search into a multi-lingual website. Now, this website contains
I'm implementing a search in my website, and would like to support searching for
I'm implementing full text search functionality on my rap website , and I'm running
I am implementing a search functionality in a website I am building, which involves
I'm implementing OpenID auth on a website and I'm using check_immediate. Now it works
I'm implementing a contact form for a website, and I'd like to avoid using
I am implementing Zend Lucene search in my website. Now I am doing my
I am implementing user setting in my rails website where a user has control

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.