Return to Snippet

Revision: 80621
at March 23, 2020 05:47 by chrisaiv


Updated URL
https://www.chrisjmendez.com/2008/06/06/as3-regular-expression-basics/

Updated Code
https://www.chrisjmendez.com/2008/06/06/as3-regular-expression-basics/

Updated Description
https://www.chrisjmendez.com/2008/06/06/as3-regular-expression-basics/

Revision: 6681
at June 6, 2008 13:12 by chrisaiv


Initial Code
//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 = "[email protected]";
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( "[email protected]" ) );


//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

Initial URL

                                

Initial Description
1. RegExp.text(string) returns a Boolean
2. RegExp.exec(string) returns an Object
3. String.search(pattern) returns an int
4. String.match(pattern) returns an Array
5. String.replace(pattern, replace) returns a String

Initial Title
AS3: Regular Expression Basics

Initial Tags
regexp

Initial Language
ActionScript 3