1
Answer

How to remove the anchor tag from a string in C#

Photo of Ashish Dhyani

Ashish Dhyani

12y
5k
1
String sample = "<a href=\"http://test.com\">LoremIpsum.Net</a> is.";
String re = @"<a [^>]+>(.*?)<\/a>";
C#
sample =Regex.Replace(sample, re, "$1"));
C#

Answers (1)

0
Photo of Manish Dwivedi
NA 8.3k 1.2m 12y
Hi Ashish,

If you want to remove anchor tag, then change little bit your regular expression like that

string sample = "<a href=\"http://test.com\">LoremIpsum.Net</a> is.";
  string re = @"<a [^>]+>(.*?)<\/a>(.*?)";

  //sample = Regex.Replace(sample, re, "$1");
  sample = Regex.Replace(sample, re,"$2");

Thanks