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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:49:43+00:00 2026-05-13T22:49:43+00:00

I have a string containing HTML and I need to replace some words to

  • 0

I have a string containing HTML and I need to replace some words to be links – I do this with the following code;

string lNewHTML = Regex.Replace(lOldHTML, "(\bword1\b|\bword2|word3\b)", "<a href=\"page.aspx#$1\">$1</a>", RegexOptions.IgnoreCase);

The code works, but I need to include some exceptions to the replace – e.g. I will not replace anything i an img-, li- and a-tag (including link-text and attributes like href and title) but still allow replacements in p-, td- and div-tags.

Can anyone figure this one out?

  • 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-13T22:49:43+00:00Added an answer on May 13, 2026 at 10:49 pm

    Ok, after some time of trying to construct a fitting regex, here my try.. This might need additional work, but should point you in the right direction.

    I am matching the words “word1” and “word2”, not inside a “tag1” or “tag2” tag. You need to adjust this to your needs, of course. Enable RegexOptions.IgnorePatternWhitespace, if you’d like to keep my formatting.

    Unfortunatly, I have come up with a regex you could simply plug into Regex.Replace, since this Regex will match the whole String since the match before, but the word you are concerned with is in the first group. This group contains index and length of the word, so you can easily replace it using String.Substring…

    (?:
        \G
        (?:
            (?>
                 <tag1(?<N>)
                |<tag2(?<N>)
                |</tag1(?<-N>)
                |</tag2(?<-N>)
                |.)*?
            (?(N)(?!))
        )*
     )
    (word1|word2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're looking for the vacuum_indexes command. http://code.google.com/appengine/docs/python/tools/uploadinganapp.html#Deleting_Unused_Indexes This will work… May 16, 2026 at 9:53 am
  • Editorial Team
    Editorial Team added an answer Using the maps on non public facing sites is against… May 16, 2026 at 9:53 am
  • Editorial Team
    Editorial Team added an answer This is impossible as transition for a modal viewController, but… May 16, 2026 at 9:53 am

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

Is it possible in ASP.NET to take a string containing some HTML and make
I need to match a string holiding html using a regex to pull out
I have a string containing a valid Clojure form. I want to replace a
I need to be able to remove non-XHTML tags from a string containing XHTML
I have a JTable instance, containing a number of rows. The columns in this
In a lot of my PHP classes, I have this code: private $strError =
I have string. There are no items to show in this view of the
I have a string like this: // string1 horse|cow|goat|zebra| and another string like this:
I have following string: <div> text0 </div> prefix <div> text1 <strong>text2</strong> text3 </div> text4
Hi guys I'm using this wonderful class here to do a bit of code

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.