如何使用ActionScript 3我linkify文本 [英] How do I linkify text using ActionScript 3

查看:146
本文介绍了如何使用ActionScript 3我linkify文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想linkify(识别的网址,并将其转换为HTML链接)的文本。该文本可以是多行,可以包含多个URL像下面的例子。

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.

我目前的动作code看起来像这样

My current actionscript code looks like this

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

与上面的脚本的问题是,它承认第一场比赛,并使用跨。另外我如何做到这一点的 @

任何帮助是非常AP preciated。

Any help is highly appreciated.

推荐答案

ooph ......为什么每个人都使用正则表达式,这些天,完成超简单的任务?同时,你忘了,那个+是一个有效字符的URL,作为替代的空间,甚至还有一个的可怕的很多可以使用其他字符,所以你的模式甚至没有相应的匹配...

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 ...

算了,反正看看<一href="http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs%5FBook%5FParts&file=12%5FUsing%5FRegular%5FEx$p$pssions%5F168%5F05.html"相对=nofollow> AS3正则表达式元字符的... 这将大大提高你的前pression的可读性,是更强大......

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

但实际点,是全球标志...

好运气,然后...:)

good luck then ... :)

这篇关于如何使用ActionScript 3我linkify文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆