I've been trying really hard to find a stable solution for a problem. I need to make all http/https links in a string as clickable links. But only those links that are in 'href' attribute of an 'a' tag, disregarding everything else.
I've been using this simple function to linkify text -
function linkify(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(exp, "<a target='_blank' href='$1'>$1</a>");
}
But the problem is that it converts links in 'src' attribute of any 'img' tag as clickable links too. Which I don't want. The string that I need to linkify can contain both 'a' as well as 'img' tags.
I even referred this link - How to replace plain URLs with links? and used this - https://github.com/cowboy/javascript-linkify, but still no luck.
Since I am using angular.js, I've also used the inbuilt 'linky' filter (https://docs.angularjs.org/api/ngSanitize/filter/linky) to linkify text but the problem still remains.
All of the above mentioned solutions linkify text in both 'a' and 'img' tags.
Looking for some help! Thanks.