How to replace anchor tag text using C#?


Balgav

I am trying to replace anchor tag value using regex

Regex.Match(link, @"<a [^>]*>(.*?)</a>").Groups[1].Value.ToString();This gives me the anchor tag text.

I tried using "Regex.replace" but I can't replace the exact text.

example:

var link="<a href="">Sample text </a>";
           var requiredText="new Text";         
           Regex.Replace(link,"<a [^>]*>(.*?)</a>",requiredText);

Please help me to solve this problem.

I'm here to thank you in advance for your help

the fourth bird

You are capturing what is in the group.

What you might do is capture what's on the left and right in the group, and use these 2 capture groups $1and $2replace:

var link=@"<a href="">Sample text </a>";
var requiredText="$1new Text$2";
link = Regex.Replace(link,"(<a [^>]*>).*?(</a>)",requiredText);
Console.WriteLine(link);
// <a href=">new Text</a>

Check out the C# demo

The regular expression will match:

  • (<a [^>]*>)capture group 1
  • .*?Match any character 0+ times
  • (</a>)capture group 2

Related


How to replace anchor tag text using C#?

Balgav I am trying to replace anchor tag value using regex Regex.Match(link, @"<a [^>]*>(.*?)</a>").Groups[1].Value.ToString();This gives me the anchor tag text. I tried using "Regex.replace" but I can't replace the exact text. example: var link="<a href="">Sa

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code but it does not update the url of the an

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code but it does not update the url of the an

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code, but it doesn't update the anchor tag's

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code, but it doesn't update the anchor tag's

How to change anchor tag's text on click using jquery?

Start I want to change the text of anchor tag on click event using jquery. Anchor Tag: <a id="view_email" href="#"><i class="fa fa-envelope"></i> View Email</a> query code: <script type="text/javascript"> $(document).ready(function(){ $("#view_email").cl

How to remove anchor tag text using java script

box How to remove anchor tag text and href links using javascriptOrJquery <a id="ancID" href="Javascript:void(0)">Here insert a dynamic text </a> I want to remove text inserted between anchor tags. Fair You can .text('')delete text by assigning an empty strin

How to remove anchor tag text using java script

box How to remove anchor tag text and href links using javascriptOrJquery <a id="ancID" href="Javascript:void(0)">Here insert a dynamic text </a> I want to remove text inserted between anchor tags. Fair You can .text('')delete text by assigning an empty strin

How to replace url in string with anchor tag

Shasha //js var content ="welcome http://www.yahoo.com?career hi http://www.yahoo.com http://www.yahoo.com http://10.179.105.223:81/Person.aspx?accountname=MHC%5CAdministrator "; //**************************************************************************

How to replace url in string with anchor tag

Shasha //js var content ="welcome http://www.yahoo.com?career hi http://www.yahoo.com http://www.yahoo.com http://10.179.105.223:81/Person.aspx?accountname=MHC%5CAdministrator "; //**************************************************************************

How to click on anchor tag in c# using gecko browser engine

Codyman I am trying to click on a link that contains the text "Next". I have tried the following code in c# using the gecko web browser engine: Gecko.DOM.GeckoLinkElement next = new Gecko.DOM.GeckoLinkElement(geckoWebBrowser1.Document.GetElementsByClassName("s

How to click on anchor tag in c# using gecko browser engine

Codyman I am trying to click on a link that contains the text "Next". I have tried the following code in c# using the gecko web browser engine: Gecko.DOM.GeckoLinkElement next = new Gecko.DOM.GeckoLinkElement(geckoWebBrowser1.Document.GetElementsByClassName("s

How to click on anchor tag in c# using gecko browser engine

Codyman I am trying to click on a link that contains the text "Next". I have tried the following code in c# using the gecko web browser engine: Gecko.DOM.GeckoLinkElement next = new Gecko.DOM.GeckoLinkElement(geckoWebBrowser1.Document.GetElementsByClassName("s

Is it possible to access the text of an anchor tag using CSS?

Alan Bertil Below is my HTML <a href="/content/testsite/en_gb/home/acis.html" class="">Branding</a> Is it possible to access the text of an anchor tag using CSS? I want something like this? The html is dynamically generated so please don't mention having an i

Is it possible to access the text of an anchor tag using CSS?

Alan Bertil Below is my HTML <a href="/content/testsite/en_gb/home/acis.html" class="">Branding</a> Is it possible to access the text of an anchor tag using CSS? I want something like this? The html is dynamically generated so please don't mention having an i

How to get text from anchor tag?

Waverly Felipe I want to get text from all anchor tags that have class="ip_click" in the page. Here's an exact example of what I'm getting: <a href="#" class="ip_click">177.1.1.1</a> <a href="#" class="ip_click">177.2.2.2</a> I want to get "177.1.1.1" and "17

How to make anchor tag clickable without text

zefum dajima I'm making a personal website linking to social media via the corresponding icons. But the link is not clickable. Here is a JSFiddle of my problem : http://jsfiddle.net/mufeeza/v9s7psf2/ <div class="linkedin icon"><a href="https://www.linkedin.com

How to get text from anchor tag?

Waverly Felipe I want to get text from all anchor tags that have class="ip_click" in the page. Here's an exact example of what I'm getting: <a href="#" class="ip_click">177.1.1.1</a> <a href="#" class="ip_click">177.2.2.2</a> I want to get "177.1.1.1" and "17

How to get text from anchor tag?

Waverly Felipe I want to get text from all anchor tags that have class="ip_click" in the page. Here's an exact example of what I'm getting: <a href="#" class="ip_click">177.1.1.1</a> <a href="#" class="ip_click">177.2.2.2</a> I want to get "177.1.1.1" and "17

How to get text from anchor tag?

Waverly Felipe I want to get text from all anchor tags that have class="ip_click" in the page. Here's an exact example of what I'm getting: <a href="#" class="ip_click">177.1.1.1</a> <a href="#" class="ip_click">177.2.2.2</a> I want to get "177.1.1.1" and "17

How to center text inside anchor tag

Arsen Mkrtchyan I want the anchor tag to look like a button and created a JsFiddle with this style .details-button { background: linear-gradient(to bottom, #FFFFFF 0, #FAB149 2%, #F89406 100%) repeat scroll 0 0 transparent; border: 1px solid #FAB149;

How to get text from anchor tag?

Waverly Felipe I want to get text from all anchor tags that have class="ip_click" in the page. Here's an exact example of what I'm getting: <a href="#" class="ip_click">177.1.1.1</a> <a href="#" class="ip_click">177.2.2.2</a> I want to get "177.1.1.1" and "17

How to get text from anchor tag?

Waverly Felipe I want to get text from all anchor tags that have class="ip_click" in the page. Here's an exact example of what I'm getting: <a href="#" class="ip_click">177.1.1.1</a> <a href="#" class="ip_click">177.2.2.2</a> I want to get "177.1.1.1" and "17

How to get text from anchor tag?

Waverly Felipe I want to get text from all anchor tags that have class="ip_click" in the page. Here's an exact example of what I'm getting: <a href="#" class="ip_click">177.1.1.1</a> <a href="#" class="ip_click">177.2.2.2</a> I want to get "177.1.1.1" and "17

How to move anchor text up inside anchor tag?

Adam Breslin I'm trying to get the anchor text to move up in the anchor tag. Here's the anchor text that says "play": I'm trying to make this anchor text that says "play" move up slightly by putting some padding at the bottom, but this doesn't seem to work whe

How to convert <text> (text tag) to anchor tag in jQuery?

Archie I have a <text>label that contains plain text, but I want to display a hyperlink instead of plain text. <svg width="800" height="600" class="overlay"> <g transform="translate(100,220)"> <path class="link" d="M80,120C120,120 120,106.66666666666666