2010-04-30 7 views
0

가능한 복제를 URL로 변환 :
How do I linkify text using ActionScript 3ActionScript를 찾아 텍스트

내가 조금 위젯의 트위터 피드와 디스플레이를 잡고이 스크립트를 가지고있다. 내가하고 싶은 것은 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); 
    } 

답변

0

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

경우 내가 어떤 말을하는 것보다, URL에 텍스트를 변환하여 정규 표현식입니다.

우선, chacacter 클래스의 거의 모든 문자가 글자 그대로 구문 분석됩니다.

그래서, 여기에

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

당신은 (/ 제외)이 문자를 검색 말한다.

이 URL 검색을위한

간단한 정규 표현식이 잘 URL 국경을 처리 할 경우 내가 잘 모르겠어요이

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

유사합니다 하지만 \b 기호는 점 할 수있다, 그래서 내가 생각 \s (공간이나 linebreak) 더 잘 어울립니다. 물론, 당신은에 맞게 조정해야

난`(? 그것은 정규 표현식의 끝없는 끝 문자열 기호를 사용하는 액션 스크립트에서 사용할 수 있습니다) 종료 확실

그리고 아닌 데이터.

관련 문제