Flash AS3 Tutorial: Convert Hyperlinks to Clickable URLs

Leave a comment Standard

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');
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s