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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:26:42+00:00 2026-06-16T03:26:42+00:00

I have HTML in a string that looks like this: <div id=control> <a href=/xx/x>y</a>

  • 0

I have HTML in a string that looks like this:

<div id="control">
    <a href="/xx/x">y</a>
    <ul>
        <li><a href="/C003Q/x" class="dw">x</a></li>
        <li><a href="/C003R/xx" class="dw">xx</a></li>
        <li><a href="/C003S/xxx" class="dw">xxx</a></li>
    </ul>
</div>

I would like to change this to the following:

<div id="control">
    <a data-href="/xx/x" ><span>y</span></a>
    <ul>
        <li><a data-href="/C003Q/x" class="dw"><span>x</span></a></li>
        <li><a data-href="/C003R/xx" class="dw"><span>xx</span></a></li>
        <li><a data-href="/C003S/xxx" class="dw"><span>xxx</span></a></li>
    </ul>
</div>

I heard about regex but I am not sure how I can use it to change something inside the address tags and to change href at the same time. Would I need to use regex twice and can I change the inside of the <a ... >...</a> using regex or is there an easier way with C#?

  • 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-16T03:26:43+00:00Added an answer on June 16, 2026 at 3:26 am

    You can use a regular expression replace. Use parentheses to catch values in the text that you match, and use $1, $2 et.c. to use the values in the replacement string:

    str = Regex.Replace(
      str,
      "<a href=\"(.+?)\" class=\"dw\">(.+?)</a>",
      "<a data-href=\"$1\" class=\"dw\"><span>$2</span></a>"
    );
    

    Note: If the HTML code doesn’t have that exact same form, the replace won’t work. If there for example is another attribute in the anchor tag, or if the attribue order is reversed, the pattern won’t match.

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

Sidebar

Related Questions

So, I have an HTML string that looks just like this: <div> <div class=venue-tooltip>
I have an html form that looks something like this: <div class=field> <input id=product_name
I have HTML that looks something like this: <div class='textbox' data-title='Sometitle'> <div class='textareaedit'> <p><strong>Test
I have model that looks like this: public class Lesson { public int Id
I have a problem that looks like this: My string of text looks like
Suppose I have an HTML snippet like this: <div> Hello <strong>There</strong> <div>I think <em>I
I have a view model with a property on it that looks like this:
I have some existing code that looks something like this: [tag:cb cbid=12345 cbwidth=200 cbclassname=calloutbox
I have a search form that looks like this: The code behind the form
I have a simple $.ajax call that returns a response that looks like this:

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.