Possible Duplicate:
How to replace plain URLs with links?
I am looking to make the user's life a little easier by parsing text input (via javascript) to find all URLs, then link them (wrap tags) so users can just click them. Facebook and even SO does something similar to give you an idea of what I'm looking for.
function censorLinks(text) {
return text.replace(
new RegExp('(https?://[^\\s]+)', 'g'),
'<a href="$1" target="_blank">$1</a>'
);
}
I don't need to support every protocol. I think http and https is find for now. I'm assuming everything beyond http://
up to the white space is a URL. This almost works but there is one tiny problem: if the URL ends with a period, the period gets included. I suppose it is a valid URL but I think it is unlikely in my situation. How can I prevent the period?