Posts Tagged ‘replace’
As I was saying in my earlier example post for regex search, I would likely post a regex replace example, and here it is! It took only a few moments to make the necessary adjustments to the previous regex search example since the code is so similar.
The original textToReplace has its text replaced by the replaceText field using the pattern from replacePattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" creationComplete="textReplace()" viewSourceURL="srcview/index.html"> <mx:Style> TextArea { cornerRadius: 5; } Application { backgroundColor: #FFFFFF; } </mx:Style> <mx:Script> <![CDATA[ public function textReplace():void { var regReplacePattern:RegExp = new RegExp(replacePattern.text); replaceResult.htmlText = textToReplace.text.replace(regReplacePattern, replaceText.text); } ]]> </mx:Script> <mx:Form x="0" y="0" width="100%" height="100%"> <mx:FormHeading label="Flex Regex Replace"/> <mx:FormItem label="Text to replace"> <mx:TextArea id="textToReplace" text="Flip-Flop" change="textReplace()"/> </mx:FormItem> <mx:FormItem label="Regex Replace pattern"> <mx:TextArea id="replacePattern" text="(\w+)-(\w+)" change="textReplace()"/> </mx:FormItem> <mx:FormItem label="Regex Text to replace with"> <mx:TextArea id="replaceText" text="$2-$1" change="textReplace()"/> </mx:FormItem> <mx:FormItem label="Replace result"> <mx:Text id="replaceResult" text="Text"/> </mx:FormItem> </mx:Form> </mx:Application> |
Example: dm_regex_replace.swf
Flex Project: dm_regex_replace.zip