1

I want to write a regex which will make link unclickable - simply removing href html tags and leaving the link url as text. So it will ignore anchor text if it is given only leave the url.

But this regex should make links unclikable if they are not from the certain domain. I want this for my private messaging system.

So regex will do following

if the link is not targeting the certain domain make the link url text. Ignore given anchor text.

asp.net 4.0 , C# 4.0

Example

<a href="http://www.monstermmorpg.com/" target="_blank">My domain</a>

This will be parsed as http://www.monstermmorpg.com/ it will be text

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • As with all regex related queries, please post sample data to test with, as well as expected results – Daryl Teo Oct 09 '11 at 23:49
  • 5
    Don't use regex to parse HTML. [Ever](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)! – Thomas Levesque Oct 09 '11 at 23:56

1 Answers1

2

Something like this works on jQuery:

$(document).ready(removeLink("domain.com"));

function removeLink(domain)
{
   $('a').each(function(){
       if($(this).attr("href")!=null)
       if($(this).attr("href").indexOf(domain)>=0)
           $(this).removeAttr("href");
})
}

Explanation:

Gets all anchor elements in the HTML rendered iterating through them and removing the href attribute of the hyperlink for all the the ones that have domain.com as part of it.

jsfiddle demo.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • actually it will remove all links expect given one. – Furkan Gözükara Oct 10 '11 at 01:03
  • thanks for comment. I solved problem with htmlagilitypack :) but jquery would also work. – Furkan Gözükara Oct 10 '11 at 01:20
  • @MonsterMMORPG my "UPDATE" had a bug, apparently. jsFiddle sometimes it's so slow that you think it ran your script and it worked but in reality it didn't. My initial example definitely works fine. I updated the link with several hyperlinks and even different paths on the same domain and it worked fine. – Icarus Oct 10 '11 at 01:22