Posts Tagged code
Flex & ActionScript Regex List
Posted by brianr in actionscript, flex, regex, tutorial on September 1st, 2010
I’m not great with regex since I don’t use it a ton so I constantly find myself looking up simple, reusable regex statements…thought I’d just start listing them as I use them in case someone else finds them useful. This will start with just a couple examples that we’ll continue to add to.
Assume the following vars:
var myString:String; var pattern:RegExp; var resultString:String; var isSuccess:Boolean;
Remove All Spaces
myString = "The quick brown fox."; pattern = /\s+/g; resultString = myString.replace(pattern, ""); trace(resultString); // Thequickbrownfox.
Remove Special Characters & Spaces
myString = "The 123 quick 456 brown !@#$% fox."; pattern = /\W/g; resultString = myString.replace(pattern, ""); trace(resultString); // The123quick456brownfox
Remove Special Characters & Numbers & Spaces
myString = "The 123 quick 456 brown !@#$% fox."; pattern = /[^a-zA-Z]+/g; resultString = myString.replace(pattern, ""); trace(resultString); // Thequickbrownfox
Test URL String
myString = "http://yahoo.com"; pattern = /^http(s)?:\/\/((\d+\.\d+\.\d+\.\d+)|(([\w-]+\.)+([a-z,A-Z][\w-]*)))(:[1-9][0-9]*)?(\/([\w-.\/:%+@&=]+[\w- .\/?:%+@&=]*)?)?(#(.*))?$/i; isSuccess = pattern.test(myString); trace(isSuccess); // true myString = "htt://yahoo.com"; isSuccess = pattern.test(myString); trace(isSuccess); // false
Trim String (With Tabs & Returns): Courtesy of Jeff Channel
myString = myString = "The quick brown fox. "; pattern = /^\s+|\s+$/gs; resultString = myString.replace(pattern, ""); trace(resultString); // "The quick brown fox."
Set Flex to Focus on Application Load
By default a Flex application is not in focus in the browser when it loads. This can be especially frustrating if you have say a login view and would like the username field to be in focus when the application starts cranking. However, with some simple JavaScript we can set the Flash object to focus when the application loads.
NOTE: This will not work in Safari, Chrome, or any other browser that leverages Webkit, as it doesn’t allow the focus to be set on embedded objects. If you’re targeting IE and Firefox the proposed solution below will work.
For example purposes, let’s assume we want to make our username TextInput field have focus when the Flex app starts rocking.
First, set the focus to the username field — assume a ViewMediator is doing this:
this.view.userNameTextInput.setFocus();
Create a simple JavaScript method to set the focus of the browser to your Flex application. Create this method in the index.template.html file in your Flex project — just drop it right before the closing HTML