How to link text using ActionScript 3

I have text that I want to link (identify URLs and convert them to HTML links). The text can be multi-line and contain multiple URLs, as shown below.

My current ActionScript code is as follows

<mx:Script>
    <![CDATA[

    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";

        //Alert.show(linkify(str),"Error");
        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 pattern:RegExp = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g;
        var match:String = pattern.exec(texty);
        return texty.replace(pattern,'<a href="' + match + '">' + 
        match + '</a>');
    }

    ]]>
</mx:Script>

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

Any help is appreciated.

0
source share
1 answer

ooph... regex , ? , , "+" URL-, , ...

, , AS3... ...

- :

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

global...

...:)

Greetz

back2dos

+1

Source: https://habr.com/ru/post/1751039/


All Articles