Flash AS3 Tutorial: Convert Hyperlinks to Clickable URLs

In my chat room I was having a heck of a time getting my regex to reliably convert hyperlinks to clickable URLs in my textarea component — sometimes it would chop off anything after a & sign or a question mark and other times it would convert text following the hyperlink into a URL as well. So I thought I would share my working solution with you. Below you’ll find a function that does the job the way it should.

private function convertURLtoHyperlinks(str:String)
{
	var urlPattern:RegExp = new RegExp("((http|https|ftp)://(\S*?.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)","ig");
	return str.replace(urlPattern, '<a href="$1" target="_blank"><u><$3></u></a>$4');
}

You may also like...

Leave a Reply