Take the 2-minute tour ×

I have a text that I want to linkify (identify URLs and convert them to HTML links). The text could be multi-line, and could contain multiple urls like the example below.

My current actionscript code looks like this


    import mx.controls.Alert;
    import mx.rpc.events.FaultEvent;      
    import mx.rpc.events.ResultEvent;

    private function init():void {
        var str:String = "@stack the website for google is http://www.google.com and gmail is http://gmail.com";

        txtStatus.htmlText = linkify(str);

     private function linkify(texty:String):String {
        //return texty.replace("/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g",function(m):String { return m.linkify(m);});
        //return texty.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(m):String {return m.linkify(m);}).replace(/(^|[^\w])(@[\d\w\-]+)/g, function(m2):String{return '@<a href="http://twitter.com/' + m2.substr(1) + '">' + m2.substr(1) + '</a>'; });
        var match:String = pattern.exec(texty);
        return texty.replace(pattern,'<a href="' + match + '">' + 
        match + '</a>');


The problem with the above script is that it recognizes the first match and uses that across. Also how do i do it for @?

Any help is highly appreciated.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

ooph ... why does everybody use regex these days, to accomplish super simple tasks? also, you forgot, that "+" is a valid character for URLs, as a replacement for space, and even an awful lot of other characters may be used, so your pattern would not even match accordingly ...

well, anyway, have a look at AS3 regex metacharacters ... that'll GREATLY improve your expression's readability and is much more robust...

i'd go with something like this, really:

var r:RegExp = /(?:http|https):\/\/\S*/g;
trace(str.replace(r, function (s:String,...rest):String { 
    return '<a href="' + s + '">' + s + '</a>' 
} ));

but the actual point, was the global flag ...

good luck then ... :)

share|improve this answer

Your Answer


