Take the 2-minute tour ×

I need to strip link tags from a body of text but keep the anchor text. for example:

<a href ="">AnchorText</a>

needs to become just:


I was considering using the following RegEx:


Is a RegEx the best way to go about this? If so, is the above RegEx pattern adequate? If RegEx isn't the way to go, what's a better solution? This needs to be done server side.

share|improve this question
Are you trying to do this at design time? run time? Is this on a page you control or which you are downloading to a client? –  Cos Callis Apr 7 '11 at 20:51
It needs to happen server side on the fly. There are multiple pages that will be formatted and all exist on the server. Eventually they will be presented as a download. –  RandomWebGuy Apr 7 '11 at 20:57
Great, looks like you found a good answer. –  Cos Callis Apr 7 '11 at 22:00
add comment

4 Answers

up vote 3 down vote accepted

Your regex will do the job. You can write it a bit simpler as


/? means 0 or 1 /

But its equivalent to your (.{0}|/)

share|improve this answer
add comment

You could just use HtmlAgilityPack:

string sampleHtml = "<a href =\"\">AnchorText</a>";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
string text = doc.DocumentNode.InnerText; //output: AnchorText
share|improve this answer
add comment

I think a regex is the best way to accomplish this, and your pattern looks like it should work.

share|improve this answer
I heard somewhere that Regex's shouldn't be used for HTML - stackoverflow.com/questions/1732348/… =D –  Tejs Apr 7 '11 at 20:50
Regex is not good for parsing html into tags, attributes and etc but in this case it is ok. –  ludesign Apr 7 '11 at 20:52
add comment

Use jQuery replaceWith:

    return $('<span/>').text($(this).text());

Assuming you are doing this on the client side.

share|improve this answer
I should have specified. I need to do this server side, otherwise I like your JQuery solution. Question edited/updated. –  RandomWebGuy Apr 7 '11 at 20:58
add comment

Your Answer


