I reviewed the answers to the basic question in this link: How to replace plain URLs with links? and have decided to use the code from what Christian Koch suggested, but it only partially covers what I need to do. I am hoping someone here can help me out.
The code Christian Koch supplied works great with one exception - When I have text that already contains links and just plain text, those links are getting a double 'a' tag, therefore causing html problems that make the text not appear right in the browser.
For instance the code works fine for this:
www.yahoo.com is a website just like http://www.google.com I see the yahoo and google text now appear as links and both have a link wrapper around them just as I would expect:
<a href="http://www.yahoo.com">www.yahoo.com</a> is a website just like <a href="http://www.google.com">http://www.google.com</a> Now take this text (contains basic text and a link already defined):
www.yahoo.com is a website just like <a href="http://www.google.com">http://www.google.com</a> When using the code supplied, the yahoo link is correct, but the google link now has a double tag:
<a href="http://www.yahoo.com">www.yahoo.com</a> is a website just like <a href="<a href="http://www.google.com">http://www.google.com</a>" target="_blank"><a href="http://www.google.com">http://www.google.com</a></a> Can someone please help me get the pattern correct so that when the text already contains a link, the pattern ignores it, but still replaces the other text without a tag. I only want the pattern to do the replacing if and only if the text is not already contained in a link tag.
Here is the code i am using from the other post:
doLinks: function(originalText) { // 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 *** here I've changed the expression *** var emailAddressPattern = /(([a-zA-Z0-9_\-\.]+)@[a-zA-Z_]+?(?:\.[a-zA-Z]{2,6}))+/gim; return originalText .replace(urlPattern, '<a target="_blank" href="$&">$&</a>') .replace(pseudoUrlPattern, '$1<a target="_blank" href="http://$2">$2</a>') .replace(emailAddressPattern, '<a target="_blank" href="mailto:$1">$1</a>'); }