2009-06-18 2 views
0

ActionScript 3에서 HTML 형식의 컨텐츠 문자열의 글꼴 속성을 구문 분석하는 방법이 궁금합니다. 다음 예제 콘텐츠 문자열을 가져 가자 :ActionScript 3을 사용하여 태그 속성을 효율적으로 구문 분석

var content:String = '<font face="Archer-Bold" size="12" color="#000000">My Content</font>'; 

나는 그 문자열을 구문 분석하고 글꼴 속성을 가진 개체를 만들고 싶습니다. 나는 정규 표현식을 추측하고있어

trace(fontInfo.name); // output: "Archer-Bold" 
trace(fontInfo.size); // output: "12" 
trace(fontInfo.color); // output: "#000000" 

갈 수있는 방법입니다,하지만 난 그들에 대해 아무것도 몰라 : 그래서 결과 객체는 다음을 추적합니다. 생각?

답변

0

이것은 내가 생각해 낸 것입니다. 정규 표현식에 이상적입니다.

<?xml version="1.0" encoding="utf-8"?> 
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
    creationComplete="onComplete();"> 

    <mx:Script> 
     <![CDATA[ 
      import mx.controls.Alert; 
      import mx.utils.ObjectUtil; 
      private var targets:Array = new Array(); 
      private function onComplete():void { 

       var face:String = null; 
       var size:String = null; 
       var color:String = null; 

       var content:String = '<font face="Archer-Bold" size="12" color="#000000">My <br> Content</font>'; 

       // since the content has invalid XML <br> tag XML construction will fail: 
       //var x:XML = new XML(content); 

       var faces:Array = content.match(/face\s*=\s*["'](.[^"']*)["']/); 
       // array is null if no matches found: 
       if (faces != null) { 
        face = faces[1]; 
       } 

       var sizes:Array = content.match(/size\s*=\s*["'](\d{1,})["']/); 
       // array is null if no matches found: 
       if (sizes != null) { 
        size = sizes[1]; 
       } 

       var colors:Array = content.match(/color\s*=\s*["'](.[^"']*)["']/); 
       // array is null if no matches found: 
       if (colors != null) { 
        color = colors[1]; 
       } 

       Alert.show("Font : " + face + ", " + size + ", " + color + "."); 
      } 
     ]]> 
    </mx:Script> 

</mx:WindowedApplication> 

희망이 있습니다.

관련 문제