代码之家  ›  专栏  ›  技术社区  ›  tim

actionscript查找文本并将其转换为url[重复]

  •  0
  • tim  · 技术社区  · 16 年前

    可能重复:
    How do I linkify text using ActionScript 3

    我有一个脚本,抓取一个twitter提要并显示在一个小部件中。我要做的是查看url的文本并将该url转换为链接。

        public class Main extends MovieClip
    {
        private var twitterXML:XML; // This holds the xml data
    
        public function Main()
        {
            // This is Untold Entertainment's Twitter id.  Did you grab yours?
            var myTwitterID= "username"; 
            // Fire the loadTwitterXML method, passing it the url to your Twitter info:
            loadTwitterXML("http://twitter.com/statuses/user_timeline/" + myTwitterID + ".xml");
        }
    
        private function loadTwitterXML(URL:String):void
        {
            var urlLoader:URLLoader = new URLLoader();
            // When all the junk has been pulled in from the url, we'll fire finishedLoadingXML:
            urlLoader.addEventListener(Event.COMPLETE, finishLoadingXML);
            urlLoader.load(new URLRequest(URL));            
        }
    
        private function finishLoadingXML(e:Event = null):void
        {
            // All the junk has been pulled in from the xml!  Hooray!
            // Remove the eventListener as a bit of housecleaning:
            e.target.removeEventListener(Event.COMPLETE, finishLoadingXML);
    
            // Populate the xml object with the xml data:
            twitterXML = new XML(e.target.data);
            showTwitterStatus();
        }
    
        private function addTextToField(text:String,field:TextField):void{
           /*Regular expressions for replacement, g: replace all, i: no lower/upper case difference
           Finds all strings starting with "http://", followed by any number of characters
           niether space nor new line.*/
           var reg:RegExp=/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
           //Replaces Note: "$&" stands for the replaced string.
           text.replace(reg,"<a href=\"$&\">$&</a>");
           field.htmlText=text;
        }
    
        private function showTwitterStatus():void
        {
            // Uncomment this line if you want to see all the fun stuff Twitter sends you:
            //trace(twitterXML);
    
            // Prep the text field to hold our latest Twitter update:
            twitter_txt.wordWrap = true;
            twitter_txt.autoSize = TextFieldAutoSize.LEFT;
    
            // Populate the text field with the first element in the status.text nodes:
            addTextToField(twitterXML.status.text[0], twitter_txt);
        }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   supervacuo    14 年前

    如果这个

    /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig
    

    是将文本转换为url的regexp,我有一些评论。

    首先,chacacter类中的几乎所有字符都被逐字解析。

    [-A-Z0-9+&@#\/%?=~_|!:,.;]
    

    你说要搜索这些字符中的任何一个(除了 / ).

    url搜索的简单regexp类似于

    /\s((https?|ftp|file):\/\/)?([-a-z0-9_.:])+(\?[-a-z0-9%_?&.])?(\s+|$)/ig
    

    \b 符号可以是一个点,所以我想 \s (空格或换行符)更适合。

    我不确定是否要结束(actionscript是否允许使用字符串结尾符号而不是regexp结尾?)

    当然,你必须调整它以适应你的数据。