Here are some Regular Expression basics in Actionscript 3.
- RegExp.text(string) returns a Boolean
- RegExp.exec(string) returns an Object
- String.search(pattern) returns an int
- String.match(pattern) returns an Array
- String.replace(pattern, replace) returns a String
//Using Replace
var toungeTwister:String = "Peter Piper Picked a peck of pickled peppers";
//g is a global identifier so it doesn't stop only on the first match
var pickRegExp:RegExp = /pick|peck/g;
var replaced:String = toungeTwister.replace( pickRegExp, "Match");
//trace(replaced);
//Using Character Classes
var compassPoints:String = "Naughty Naotersn elephants squirt water";
var firstWordRegExp:RegExp = /N(a|o)/g;
//trace( compassPoints.replace( firstWordRegExp, "MATCH" ) );
var favoriteFruit = "bananas";
var bananaRegExp:RegExp = /b(an)+a/;
//trace( bananaRegExp.test( favoriteFruit ) );
//Exec() method returns an Object containing the groups that were matched
var htmlText:String = "<strong>This text is important</strong> while this text is not as important <strong>ya</strong>";
var strongRegExp:RegExp = /<strong>(.*?)<\/strong>/g;
var matches:Object = strongRegExp.exec( htmlText);
for( var i:String in matches ) {
//trace( i + ": " + matches[i] );
}
var email:String = "c@chrisaiv.comm";
var emailRegExp:RegExp = /^([a-zA-Z0-9_-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})$/i;
var catches:Object = emailRegExp.exec( email );
for( var j:String in catches ) {
//trace( j + ": " + catches[j] );
}
//trace( "This e-mail's validity is: " + emailRegExp.test( email ) );
//Test the validity of an e-mail
var validEmailRegExp:RegExp = /([a-z0-9._-]+)@([a-z0-9.-]+)\.([a-z]{2,4})/;
trace( validEmailRegExp.test( "a1a@c.info" ) );
//Return a Boolean if there is a pattern match
var phoneNumberPattern:RegExp = /\d\d\d-\d\d\d-\d\d\d\d/;
trace( phoneNumberPattern.test( "347-555-5555" )); //true
trace( phoneNumberPattern.test("Call 800-123-4567 now!")); //true
trace( phoneNumberPattern.test("Call now!")); //false
//Return the index number of the occurence is there is a pattern match
var themTharHills:String = "hillshillshillsGOLDhills";
trace(themTharHills.search(/gold/i)); //15
AS3: Use HTML and JavaScript to send Text to Flash
This is the first of two ways to send Text to Flash using HTML and JavaScript. To follow my example, you must download and link to [SWFObject](http:...
AS3: Regular Expression Basic example
The first example strips out the HTML. The second example shows how to target a single quote using unicode. //////////////////////////////////////////// //Example 1: //////////////////////////////////////////// var re:RegExp = /<font face=\u0022(...
AS2: Inline CSS
This shows how to create Cascading Style Sheets using ActionScript. /************************** CSS Translation of Properties **************************/ /* HTML ActionScript color color display display font-family fontFamily font-size fontSize font-weight fontWeight margin-left marginLeft...