I'm trying to wrap any url that is in some text and turn it into a hyperlink ... but I don't want to wrap a url that is already wrapped with a hyperlink.
For instance:
<a href="http://twitter.com">Go To Twitter</a> here is a url http:
The following code:
function replaceURLWithHTMLLinks(text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@
Gives the following output:
<a href="<a href='http://twitter.com/twitter'>http://twitter.com/twitter</a>">@BIR</a> <a href="http://anotherurl.com">http://anotherurl.com</a>
How to change the regular expression to exclude already hyperlinks to URL?
thanks
Answer:
New Method:
function replaceURLWithHTMLLinks(text) { var exp = /(?:^|[^"'])((ftp|http|https|file):\/\/[\S]+(\b|$))/gi return text.replace(exp, " <a href='$1'>$1</a>"); }
The code above functions as needed. I changed the regular expression from the link in the comments because it contained an error in which it would include a full stop, now it excludes any full stops that appear after the full URL.
source share