Reputation:
In an AIR app, I am loading a bunch of arbitrary text via JSON, loading it into an object, and displaying it via a custom renderer. I would like to have urls be clickable. So I'm sure that this is possible via some crazy regex thing (as I found for php here), but, Flex being flex, I'm astonished that there isn't some builtin functionality for this that I'm just not finding, or, failing that, a library that someone has created to do just this.
(I'm equally astonished that this question hasn't been asked here before. I anticipate being flamed with the link to that)
Failing that, anyone want to help with some crazy regex? ;>
Thanks in advance!
Upvotes: 1
Views: 1658
Reputation: 21
I would like to add that the following RegEx could be much more useful for validating an URL:
/(((f|ht){1}tp:\/)[-a-zA-Z0-9@:%_\+.~#?&\/=]+)/g
Upvotes: 2
Reputation: 2379
You could replace the URLs in your text for actual links using the following regex:
str = str.replace(/((https?|ftp|telnet|file):((\/\/)|(\\\\))+[\w\d:#@%\/;$()~_?\+-=\\\.&]*)/g, "<u><a href='$1'>$1</a></u>");
Then set the htmlText on a Label
or Text
component and listen for it's link
event:
<mx:Text htmlText="{str}" link="linkHandler(event)"/>
Then open the URL on the handler:
public function linkHandler(event:TextEvent):void {
navigateToURL(new URLRequest(event.text), '_blank');
}
Except for that regex, I haven't tested this code, but it should work. Also, this could be of some help to you.
Upvotes: 3