Possible Duplicate:
How to replace plain URLs with links?
I have several strings that have links in them. For instance:
var str = "I really love this site: http://www.stackoverflow.com"
and I need to add a link tag to that so the str will be:
I really love this site: <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a>
I imagine there would be some regex involved, but I can't get it to work for me with match(). Any other ideas
That's easy:
str.replace( /(http:\/\/[^\s]+)/gi , '<a href="$1">$1</a>' )
Output:
I really love this site: <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a>
function replaceURL(val) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return val.replace(exp,"<a href='$1'>$1</a>");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With