This is a very helpful piece of code. Function to escape all regular expression special chars. I came to this string having this order of characters by trial and error. So now you can use it easily by copy and paste.
What is important to know that MXML and Actionscript compiler do automatically escapes backslashes (“\”) and because of this you may encounter a lot of problems with regular expression.
This piece of code makes use a lot of regular expression so to fully understand the expression you really need to get into regular expression “language”.
So here it is…
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 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="200"> <mx:TextArea id="oldstr" text="{}()^$&.*?-/+|[]\" x="10" y="10" width="380" height="70"/> <mx:TextArea id="newstr" x="10" y="118" width="380" height="70"/> <mx:Button x="149" y="88" label="Escape string" click="handleDoEscape(event)"/> <mx:Script> <![CDATA[ // string to test with: {}()^$&.*?-/+|[]\ private function escapeRegexChars(s:String):String { var newString:String = s.replace( new RegExp("([{}\(\)\^$&.\*\?\/\+\|\[\\\\]|\]|\-)","g"), "\\$1"); return newString; } private function handleDoEscape(event:Event):void { newstr.text = escapeRegexChars(oldstr.text); } ]]> </mx:Script> </mx:Application> |
Easy codding… and learn Regexp!
Useful site for learning and more: http://www.regular-expressions.info/
Tags: ActionScript, MXML, Regexp, regular expression
This post was written by Andrei Ionescu
Views: 5947










HI,
I am new to flex and working on some project.
I have a search filed in my page and i have to search for the data in the grid present in the same page, i am using regular expression for searching the data,
i also have some special characters in the grid data,
to escape the characters i used the function u provided in this link,
it worked fine for all the special characters except ” $, ^ and . ”
Can u please help me out of this by letting me know how to escape these characters.
Thankyou.
Hey..
Thanks a lot for this RegEx… I was having a hard time to find an escape sequence for a piece of string. You really decreased my efforts a lot.
Thanks