Replace string block containing a URL with html hyperlink,

Hi there. The basic idea is that I have a string that contains a user’s comment, which could potentially contain URLs, so I wanted to take a string like:

“Gee, the folks in the http://www.kirupa.com forum are so nice.”

And using pattern replace, replace the URL with an HTML hyperlink, so the string would end up being:

“Gee, the folks in the <a href=‘http://www.kirupa.com’>http://www.kirupa.com</a> forum are so nice.”

I have found code that works perfectly, but only if the string is ONLY a url:


var protocol:String = "((?:http|ftp)://)"; 
var urlPart:String = "([a-z0-9_-]+\.[a-z0-9_-]+)"; 
var optionalUrlPart:String = "(\.[a-z0-9_-]*)"; 
var urlPattern:RegExp = new RegExp(protocol + urlPart + optionalUrlPart, "gi"); 
newString = originalString.replace(urlPattern, "<a href='$1$2$3'>$1$2$3</a>");

So the above code would work perfectly if the original string were:

http://www.kirupa.com

So my question is how can I alter the pattern replace code so that it accommodates a string containing random text, as well as a URL.

Any thoughts would be very much appreciated. Thank you.