Furkan Gözükara
Furkan Gözükara

Reputation: 23850

Make link unclickable with regex unless from the certain domain

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

Upvotes: 1

Views: 270

Answers (1)

Icarus
Icarus

Reputation: 63964

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.

Upvotes: 2

Related Questions