ActionScript正则表达式测试工具,使用Flex编写

为了在开发过程中,测试正则表达式更方便,用Flex编写了下面的正则表达式测试工具,和大家分享,欢迎提出改进意见

改进历史
2009-9-17
  正则表达式输入时即时匹配,并用特殊的样式显示匹配的文本

在独立的窗口中查看运行RegexTester


现在样例比较少,今后将逐渐增加
下载MXML代码
  1 <?xml version="1.0" encoding="utf-8"?>
  2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="left"
  3     backgroundColor="white" fontSize="12" creationComplete="requestExamplesHandler()">
  4     
  5     <mx:Array id="options">
  6         <mx:Object label="全部匹配" data="g"/>
  7         <mx:Object label="忽略大小写" data="i"/>
  8         <mx:Object label="匹配多行" data="m"/>
  9         <mx:Object label="匹配换行符" data="x"/>
 10         <mx:Object label="是否允许扩展点表达式" data="e"/>
 11     </mx:Array>
 12     
 13     <mx:ApplicationControlBar dock="true">
 14         <mx:Label text="选项:"/>
 15         <mx:Repeater id="repeaterOptions" dataProvider="{options}">
 16             <mx:CheckBox id="checkboxOption" label="{repeaterOptions.currentItem.label}"
 17                 selected="true" click="optionClick(event)"/>
 18         </mx:Repeater>
 19     </mx:ApplicationControlBar>
 20     
 21     <mx:HDividedBox width="100%" height="100%">
 22         <mx:DividedBox width="70%" height="100%">
 23             
 24             <mx:VBox width="100%" height="15%" minHeight="75">
 25                 <mx:HBox>
 26                     <mx:Label text="Pattern:"/>
 27                     <mx:Spacer width="100%"/>
 28                     <mx:Button label="match" click="matchHandler(event)"/>
 29                     <mx:Button label="execute" click="executeHandler(event)"/>
 30                 </mx:HBox>
 31                     
 32                 <mx:TextArea id="textPattern" width="100%" height="100%"/>
 33             </mx:VBox>
 34             
 35             <mx:VBox width="100%" height="15%" minHeight="75">
 36                 <mx:HBox>
 37                     <mx:Label text="Replace with:"/>
 38                     <mx:Button label="replace" click="replaceHandler(event)"/>
 39                 </mx:HBox>
 40                 <mx:TextArea id="textReplaceWith" width="100%" height="100%"/>
 41             </mx:VBox>
 42             
 43             <mx:VBox width="100%" height="20%" minHeight="75">
 44                 <mx:Label text="Search Text:"/>
 45                 <mx:TextArea id="textSearch" width="100%" height="100%"/>
 46             </mx:VBox>
 47             
 48             <mx:VBox width="100%" height="50%" minHeight="75">
 49                 <mx:Label text="Result:"/>
 50                 <mx:TextArea id="textResult" width="100%" height="100%"/>
 51             </mx:VBox>
 52             
 53         </mx:DividedBox>
 54         
 55         <mx:VBox width="30%" height="100%">
 56             <mx:Label text="Examples:"/>
 57             <mx:List id="dataExamples" dataProvider="[1,2,3]" width="100%" height="100%"
 58                 labelField="title" itemClick="itemClickHandler(event)"/>
 59         </mx:VBox>
 60             
 61     </mx:HDividedBox>
 62     
 63     <mx:HTTPService id="hsExamples" url="assets/examples.xml" resultFormat="e4x"
 64         result="resultHandler(event)"/>
 65     
 66     <mx:Script>
 67         <![CDATA[
 68             import mx.controls.Alert;
 69             import mx.rpc.events.ResultEvent;
 70             
 71             private var patterOptions:String = "gimex";
 72             
 73             private function optionClick(event:MouseEvent):void
 74             {
 75                 patterOptions = "";
 76                 for (var i:uint = 0; i < checkboxOption.length; i++)
 77                 {
 78                     if (checkboxOption[i].selected)
 79                         patterOptions += options[i].data;
 80                 }
 81                 trace(patterOptions);
 82             }
 83             
 84             private function matchHandler(event: MouseEvent):void
 85             {
 86                 var pattern:RegExp = new RegExp(textPattern.text, patterOptions);
 87                 var search:String = textSearch.text;
 88                 var results:Array = search.match(pattern);
 89                 
 90                 textResult.text = results.join("\n");
 91             }
 92             
 93             private function executeHandler(event:MouseEvent):void
 94             {
 95                 var pattern:RegExp = new RegExp(textPattern.text, patterOptions);
 96                 var search:String = textSearch.text;
 97                 
 98                 var result: Array = pattern.exec(search);
 99                 textResult.text = "";
100                 var i:uint = 1;
101                 var lastIndex:uint = pattern.lastIndex;
102                 while (result != null)
103                 {
104                     textResult.text += "匹配:" + i++.toString();
105                     for (var prop:String in result)
106                     {
107                         textResult.text += "\n\t" + prop + ": \t" + result[prop];
108                     }
109                     textResult.text += "\n---------------------------------------------------------\n";
110                     //如果在不是global的情况下,lastIndex始终是0,将导致无限循环,因此,只能执行1次
111                     if (pattern.lastIndex == 0)
112                         break;
113                         
114                     result = pattern.exec(search);
115                     //两次匹配后lastIndex相等,说明出现了死循环,应该退出
116                     if (pattern.lastIndex == lastIndex) 
117                         break;
118 
119                     lastIndex = pattern.lastIndex;
120                 }
121                 
122             }
123             
124             private function replaceHandler(event: MouseEvent):void
125             {
126                 var pattern:RegExp = new RegExp(textPattern.text, patterOptions);
127                 var replaceWith: String = textReplaceWith.text;
128                 var search:String = textSearch.text;
129                 textResult.text = search.replace(pattern, replaceWith);
130             }
131             
132             private function requestExamplesHandler():void
133             {
134                 hsExamples.send();
135             }
136             
137             private function resultHandler(event:ResultEvent):void
138             {
139                 dataExamples.dataProvider = event.result.item;
140             }
141             
142             private function itemClickHandler(event: Event):void
143             {
144                 var item:Object = event.target.selectedItem;
145                 textPattern.text = item.pattern;
146                 textReplaceWith.text = item.replace;
147                 textSearch.text = item.search;
148             }
149         ]]>
150     </mx:Script>
151     
152 </mx:Application>
153 
154 
posted @ 2009-09-15 17:57  静候良机  阅读(2393)  评论(8编辑  收藏  举报