September 21, 2010

Sanitize String for Regular Expression

I recently came across a need to use a user supplied input in a regular expression. To prevent any special characters in the user's string from being treated as a regular expression control character I stumbled upon this solution. Alas I can't remember where I found it but wanted to share.

/**
* Convert a random string into a format that will make sure it doesn't
* impact a regular expression when inserted into it.
* @param string String to sanitize.
* @return Sanitized string
*/
public function sanitizeForRegExp(string:String):String
{
    if (string == null)
    {
        return null;
    }
    return string.replace(new RegExp("([{}\(\)\^{{content}}amp;.\*\?\/\+\|\[\\\\]|\]|\-)","g"), "\\$1");
}

Tags: as3 flex regexp

February 26, 2007

RegExp Constructor Issue

The Flex documentation is a a little misleading in regards to the RegExp Constructor. Livedocs has this to say:

public function RegExp(re:String, flags:String)

Lets you construct a regular expression from two strings. One string defines the pattern of the regular expression, and the other defines the flags used in the regular expression.

Notice the lack of a default value for the flags argument? One would think that would mean you should get a compilation error if you don't supply both arguments to the constructor. However, both of these compile without error:

var regExpA:RegExp = new RegExp("^http://www.google.com/(.*)$");
var regExpB:RegExp = new RegExp("^http://www.google.com/(.*)$", "");

I would expect the first one to cause this error:

1136: Incorrect number of arguments. Expected 2.

It would be nice if the documentation was just wrong and you didn't need to supply the second argument to the constructor. The problem is the two regular expressions behave differently! The missing flags argument causes the regular expression to do funky stuff, which is not the same as passing in the empty string. For example look at the output of this little example:

trace(regExpA.test("http://www.google.com/"));
trace(regExpA.test("http://www.google.com/webhp?hl=en"));
trace(regExpB.test("http://www.google.com/"));
trace(regExpB.test("http://www.google.com/webhp?hl=en"));
// Output
// true
// false
// true
// true

That false shouldn't be there! The funky stuff that is going on behind the scenes is that without the second argument to the constructor it is looking at the regular expression string itself to set the flags. In this case g appears after a //, which is the way you specify the global flag in a RegExp literal. To confirm this, look at the string version of each RegExp:

trace(regExpA);
trace(regExpB);
// Output
// /^http://www.google.com/(.*)$/gm
// /^http://www.google.com/(.*)$/

Cool, we also picked up the multiline flag since m also appears! The problem only occurs if you have more than one / followed by one of the regular expression flags. The two slahses don't need to be next to each other just having two anywhere in the string followed by other text can cause the problem. In general you are probably better using RegExp literals, but if you do use the RegExp constructor be sure to supply both arguments, even if the second one is just the empty string.

Tags: error flex regexp

June 30, 2006

Regular Expression Tester Updated

With the public release of Flash player 9, I've updated the regular expression tester to use the latest version.

I also fixed a bug with the display of optional groups.

NOTE: If the screen is blank, that probably means that you don't have the correct Flash version. Stop by the Flash version page and make sure you have at least 9.0.15.0 installed. If you don't you can easily download it.

Launch RegExp Tester!

Tags: flex regexp

June 30, 2006

Regular Expressions

I've been playing around with Flex 2 for some time at work. One of the neat things that Actionscript 3 adds is native regular expressions. As part of an ongoing effort to share knowledge I volunteered to give an informal talk about regular expressions. To help with the talk I created this visual regular expression viewer. It helps show you what matched along with any groupings and other standard stuff. Additionally it can also perform a regular expression replace operation or a simple split operation.

You will need the Flash 9 in order to run the application.

Launch RegExp Tester!

If you find any bugs, have comments, etc. please leave me a comment.

Tags: actionscript as3 programming regexp regularexpression