I am using regex to replace all url
in string to <a href="url">url</a>
.
For that I am using
if(!String.linkify) {
String.prototype.linkify = function() {
// http://, https://, ftp://
var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
// www. sans http:// or https://
var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
// Email addresses
var emailAddressPattern = /[\w.]+@[a-zA-Z_-]+?(?:\.[a-zA-Z]{2,6})+/gim;
return this
.replace(urlPattern, '<a href="$&">$&</a>')
.replace(pseudoUrlPattern, '$1<a href="http://$2">$2</a>')
.replace(emailAddressPattern, '<a href="mailto:$&">$&</a>');
};
}
and
mystr.linkify();
it's from here , https://stackoverflow.com/a/7123542/1602333
it works good unless I have img tag in my string , it replaces
<img src="imgurl"/>
to <img src="<a href="imgurl">imgurl</a>"
Which change should I do to my regex to not replace urls which are part of some tag attribute ?
Any help would be great.