ActionScript 3 AS3:正则表达式基础

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3:正则表达式基础相关的知识,希望对你有一定的参考价值。

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

以上是关于ActionScript 3 AS3:正则表达式基础的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3:正则表达助手

ActionScript 3 AS3使用正则表达式删除回车符和新行

ActionScript 3 AS3使用正则表达式检查并返回有效的英国邮政编码

ActionScript 3 中的正则表达式:如何排除复杂前缀?

AS3:正则表达式基础

AS3:正则表达式基本示例