Flash ActionScript strip slashes using Regular Expressions
I am just having a bit of a play with some ActionScript 2 for an upcoming project. I have came across the necessity to strip slashes of a string that has been directly parsed from PHP. As it is a object containing many properties it may seem unfeasible to try and strip slashes for every property of the whole object. Instead I will look in flash on how to allow the Flash Developer to strip slashes when they wish to use the object property.
To do so we will want to have a look at Regular Expressions in Flash. Flash did not come packaged with any Regular Expression classes by default, so we will have a look at a using a popular package “RegExp.as” provided by Joey Lott at this URL: http://www.jurjans.lv/flash/RegExp.html. Once we have downloaded the package from their website, you can set it up in your flash file using a simple import.
Reading over the documentation to the behaviour of “addslashes” on PHP.net:
Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote (‘), double quote (“), backslash (\) and NUL (the NULL byte).
So our strip slashes will atleast need to ensure that the escaped characters do not contain escaping ‘\’ characters.
One basic way of doing this would be to create a Regular Expression that would match against those characters.
Regular Expression to match occurance of addslashes escaping
(\\([”‘\\])) - Starts with a slash followed by any of the escaped characters (NOTE: this contains single quote and double quote characters which need to be escaped)
The escaped value to use in our Flash Regular expression is “(\\\\([\”\’\\\\]))” (a mouth full of escaping due to the fact that we are matching escaped characters).
// FLASH ActionScript Strip Slashes Example
// Import our RegExp class
import RegExp;
// Some escaped text that has arived from PHP or a database
body = "\"foobar.html\" and then \\ \' this and that etc";
trace(stripslashes(body));
/**
* Strips slashes of a String
* @param str String Escaped String to strip slashes
* @return String with slashes removed
*/
function stripslashes(str:String):String {
var stripSlashesRegExp = new RegExp("(\\\\([\"\'\\\\]))","gim");
return str.replace(stripSlashesRegExp, "$2");
}
This will be much more powerful than a split(’\\’).join() style as that method will strip all backslashes in your document. You may want to consider using a wrapper class and create a Singleton design pattern to stop compiling the regular expression every time it is called. Maybe you could consider adding it as a prototype for the String Class also.






Just on utility classes and functions, for simple projects it’s easy just to extend the properites of String objects using prototyping (or any Flash class to that matter).
Eg
// Creating the prototype String.prototype.myFunction = function() { return this.concat(" World"); };Comment by Richard Lee — July 6, 2006 @ 10:56 am
Prototyping I don’t think is supported in AS2 but is a cool trick (and a fundamental of this RegExp class). I am going to have to hunt for an ActionScript 2 compliant Regular Expression class. If anyone knows one please leave a comment.
When reading in HTML from a remote source you may need to strip new line entries to stop un-necessary paragraph and line breaks. We can once again do this with Regular Expressions.
To strip un-necessary line endings from a string, use “[\n\r]” (you will need to escape) and replace with “”.
Comment by Cameron Manderson — July 6, 2006 @ 12:52 pm