一个在Flash里面使用的正则表达式的类,使用这个类的原因是FLASH里面对文本输入检测要求不高,例如就不能检测输入的邮件格式是否正确;
classRegExp{
publicvarconst:String=null;
publicvarsource:String=null;
publicvarglobal:Boolean=false;
publicvarignoreCase:Boolean=false;
publicvarmultiline:Boolean=false;
publicvarlastIndex:Number=null;
publicstaticvar_xrStatic:Number=null;
publicvar_xr:Number=null;
publicstaticvar_xp:Number=null;
publicstaticvar_xxa:Array=null;
publicstaticvar_xxlp:Number=null;
publicvar_xq:Number=null;
publicvar_xqc:Number=null;
publicstaticvard:Number=null;
publicstaticvar_xiStatic:Number=null;
publicvar_xi:Number=0;
publicstaticvar_xxlm:String=null;
publicstaticvar_xxlc:String=null;
publicstaticvar_xxrc:String=null;
publicstaticvarlastMatch:String=null;
publicstaticvarleftContext:String=null;
publicstaticvarrightContext:String=null;
publicstaticvar_xa:Array=newArray();
publicstaticvarlastParen:String=null;
publicstaticvar_xaStatic:Array=newArray();
publicstaticvar$1:String=null;
publicstaticvar$2:String=null;
publicstaticvar$3:String=null;
publicstaticvar$4:String=null;
publicstaticvar$5:String=null;
publicstaticvar$6:String=null;
publicstaticvar$7:String=null;
publicstaticvar$8:String=null;
publicstaticvar$9:String=null;
privatestaticvar_setString:Boolean=RegExp.setStringMethods();
functionRegExp(){
if(arguments[0]==null){
}else{
const="RegExp";
compile.apply(this,arguments);
}
}
publicfunctioninvStr(sVal:String):String{
vars=sVal;
varl=length(s);
varj;
varc;
varr="";
for(vari=1;i<255;i++){
c=chr(i);
j=0;
while(j<=l&&substring(s,1+j++,1)!=c){
}
if(j>l){
r+=c;
}
}
returns;
}
publicfunctioncompile(){
this.source=arguments[0];
if(arguments.length>1){
varflags=(arguments[1]+’’).toLowerCase();
for(vari=0;i<length(flags);i++){
if(substring(flags,i+1,1)=="g"){
this.global=true;
}
if(substring(flags,i+1,1)=="i"){
this.ignoreCase=true;
}
if(substring(flags,i+1,1)=="m"){
this.multiline=true;
}
}
}
if(arguments.length<3){
varroot=true;
RegExp._xrStatic=1;
//Parencounter
vari=0;
}else{
varroot=false;
this._xr=RegExp._xrStatic++;
vari=arguments[2];
}
this.lastIndex=0;
/*
Compiletheregularexpression
Thearrayofcharacterdefinitionobjectswillbecreated:
q[n].t-->typeofmatchrequired:0=exact
1=incharset
2=notincharset
3=paren
4=reftoparen
7=new"OR"section
9=beginningofline
10=endofline
q[n].s-->characterorcharacterset
q[n].a-->characterhastorepeatatleastatimes
q[n].b-->characterhastorepeatatmostbtimes
*/
varre=this.source;
varex;
varl=length(re);
varq=[];
varqc=0;
vars;
varrange=false;
varca;
varcb;
varatEnd=false;
varchar;
for(i=i;i<l;++i){
varthischar=substring(re,i+1,1);
if(thischar=="\\"){
i++;
char=false;
thischar=substring(re,i+1,1);
}else{
char=true;
}
varnextchar=substring(re,i+2,1);
q[qc]=newObject();
q[qc].t=0;
q[qc].a=0;
q[qc].b=999;
q[qc].c=-10;
if(char){
//Handlespecialcharacters
if(thischar=="("){
//Openingparen
ex=newRegExp(re,(this.ignoreCase?"gi":"g"),i+1);
i=RegExp._xiStatic;
q[qc].t=3;
thischar=ex;
nextchar=substring(re,i+2,1);
}elseif(!root&&thischar==")"){
//Closingparen
break;
}elseif(thischar=="^"){
//Mustbelocatedatthebeginningofstring/line
if(qc==0||q[qc-1].t==7){
q[qc].t=9;
q[qc].a=1;
q[qc].b=1;
qc++;
}
continue;
}elseif(thischar=="$"){
//Mustbelocatedattheendofstring/line
if(root){
atEnd=true;
}
continue;
}elseif(thischar=="["){
//Thisisacharacterset
i++;
if(nextchar=="^"){
q[qc].t=2;
i++;
}else{
q[qc].t=1;
}
thischar="";
range=false;
while(i<l&&(s=substring(re,1+i++,1))!="]"){
if(range){
//Previouscharwas"-",socreatearange
if(s=="\\"){
}
cb=s=="\\"?(s=="b"?chr(8):substring(re,1+i++,1)):s;
ca=ord(substring(thischar,length(thischar),1))+1;
while(cb>=(s=chr(ca++))){
thischar+=s;
}
range=false;
}else{
if(s=="-"&&length(thischar)>0){
//Characterrangeisbeingdefined
range=true;
}else{
if(s=="\\"){
//Predefinedcharsetmayfollow
s=substring(re,1+i++,1);
if(s=="d"){
thischar+="0123456789";
}elseif(s=="D"){
thischar+=invStr("0123456789");
}elseif(s=="s"){
thischar+="\f\n\r\t\v";
}elseif(s=="S"){
thischar+=invStr("\f\n\r\t\v");
}elseif(s=="w"){
thischar+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
}elseif(s=="W"){
thischar+=invStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_");
}elseif(s=="b"){
thischar+=chr(8);
}elseif(s=="\\"){
thischar+=s;
}
}else{
thischar+=s;
}
}
}
}
if(range)thischar+="-";
i--;
varnextchar=substring(re,i+2,1);
}elseif(thischar=="|"){
//ORsection
if(atEnd){
q[qc].t=10;
q[qc].a=1;
q[qc].b=1;
qc++;
q[qc]=newObject();
atEnd=false;
}
q[qc].t=7;
q[qc].a=1;
q[qc].b=1;
qc++;
continue;
}elseif(thischar=="."){
q[qc].t=2;
thischar="\n";
}elseif(thischar=="*"||thischar=="?"||thischar=="+"){
continue;
}
}else{
if(thischar>="1"&&thischar<="9"){
q[qc].t=4;
}elseif(thischar=="b"){
q[qc].t=1;
thischar="--wb--";
}elseif(thischar=="B"){
q[qc].t=2;
thischar="--wb--";
}elseif(thischar=="d"){
q[qc].t=1;
thischar="0123456789";
}elseif(thischar=="D"){
q[qc].t=2;
thischar="0123456789";
}elseif(thischar=="s"){
q[qc].t=1;
thischar="\f\n\r\t\v";
}elseif(thischar=="S"){
q[qc].t=2;
thischar="\f\n\r\t\v";
}elseif(thischar=="w"){
q[qc].t=1;
thischar="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
}elseif(thischar=="W"){
q[qc].t=2;
thischar="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
}
}
//Countingmetacharacters
if(nextchar=="*"){
q[qc].s=thischar;
qc++;
i++;
}elseif(nextchar=="?"){
q[qc].s=thischar;
q[qc].b=1;
qc++;
i++;
}elseif(nextchar=="+"){
q[qc].s=thischar;
q[qc].a=1;
qc++;
i++;
}elseif(nextchar=="{"){
varcomma=false;
varrangeA=0;
range="";
i++;
while(i+1<l&&(s=substring(re,2+i++,1))!="}"){
if(!comma&&s==","){
comma=true;
rangeA=Number(range);
rangeA=Math.floor(isNaN(rangeA)?0:rangeA);
if(rangeA<0){
rangeA=0;
}
range="";
}else{
range+=s;
}
}
varrangeB=Number(range);
rangeB=Math.floor(isNaN(rangeB)?0:rangeB);
if(rangeB<1){
rangeB=999;
}
if(rangeB<rangeA){
rangeB=rangeA;
}
q[qc].s=thischar;
q[qc].b=rangeB;
q[qc].a=comma?rangeA:rangeB;
qc++;
}else{
q[qc].s=thischar;
q[qc].a=1;
q[qc].b=1;
qc++;
}
}
if(root&&atEnd){
q[qc]=newObject();
q[qc].t=10;
q[qc].a=1;
q[qc].b=1;
qc++;
}
if(!root){
RegExp._xiStatic=i;
this.source=substring(re,arguments[2]+1,i-arguments[2]);
}
if(RegExp.d){
for(vari=0;i<qc;i++){
trace("xr"+this._xr+’’+q[i].t+":"+q[i].a+":"+q[i].b+":"+q[i].s);
}
}
this._xq=q;
this._xqc=qc;
RegExp._xp=0;
}
publicfunctiontest(){
if(RegExp._xp++==0){
RegExp._xxa=[];
//Temparrayforstoringparenmatches
RegExp._xxlp=0;
//Lastparen
}
//q[n].c-->countofmatches
//q[n].i-->indexwithinthestring
varstr=arguments[0]+’’;
varre;
varq=this._xq;
varqc=this._xqc;
varqb;
varc;
varcl;
varct;
vars;
varl=length(str);
varix=this.global?this.lastIndex:0;
varix_=ix;
varstr_=str;
if(this.ignoreCase){
str=str.toLowerCase();
}
varr=newObject();
r.i=-1;
vari=-1;
while(i<qc-1){
i++;
if(RegExp.d){
trace("Newsectionstartedati="+i);
}
ix=ix_;
qb=i;
q[qb].c=-10;
varatEnd=false;
while(i>qb||ix<l+1){
if(q[i].t==7){
//New"OR"sectioncoming
break;
}elseif(q[i].t==9){
i++;
if(i==qb+1){
varatStart=true;
qb=i;
}
q[qb].c=-10;
continue;
}else{
if(r.i>=0&&ix>=r.i){
//Thereisalreadybettermatch,soquitsearching
break;
}
if(q[i].c==-10){
if(RegExp.d){
trace("Lookup#"+i+"atindex"+ix+"for\\\\\\\\\\\\\\\\’"+q[i].s+"\\\\\\\\\\\\\\\\’type"+q[i].t);
}
//Countthe#ofmatches
varm=0;
q[i].i=ix;
if(q[i].t==0){
//Exactmatch
c=this.ignoreCase?q[i].s.toLowerCase():q[i].s;
while(m<q[i].b&&ix<l){
if(substring(str,1+ix,1)==c){
m++;
ix++;
}else{
break;
}
}
}elseif(q[i].t==1){
//Incharset
if(q[i].s=="--wb--"){
q[i].a=1;
if(ix>0&&ix<l){
ct=substring(str,ix,1);
if(ct==""||ct=="\\\\\\\\\\\\\\\\n"){
m=1;
}
if(m==0){
ct=substring(str,1+ix,1);
if(ct==""||ct=="\\\\\\\\\\\\\\\\n"){
m=1;
}
}
}else{
m=1;
}
}else{
c=this.ignoreCase?q[i].s.toLowerCase():q[i].s;
cl=length(c);
varcs;
while(m<q[i].b&&ix<l){
ct=substring(str,1+ix,1);
cs=0;
while(cs<=cl&&substring(c,1+cs++,1)!=ct){
}
if(cs<=cl){
m++;
ix++;
}else{
break;
}
}
}
}elseif(q[i].t==2){
//Notincharset
c=this.ignoreCase?q[i].s.toLowerCase():q[i].s;
cl=length(c);
if(q[i].s=="--wb--"){
q[i].a=1;
if(ix>0&&ix<l){
ct=substring(str,ix,1);
s=substring(str,1+ix,1);
if(ct!=""&&ct!="\\\\\\\\\\\\\\\\n"&&s!=""&&s!="\\\\\\\\\\\\\\\\n"){
m=1;
}
}else{
m=0;
}
}else{
while(m<q[i].b&&ix<l){
ct=substring(str,1+ix,1);
cs=0;
while(cs<=cl&&substring(c,1+cs++,1)!=ct){
}
if(cs<=cl){
break;
}else{
m++;
ix++;
}
}
}
}elseif(q[i].t==10){
//Endofstring/linemustbenext
s=substring(str,1+ix,1);
m=(this.multiline&&(s=="\\\\\\\\\\\\\\\\n"||s=="\\\\\\\\\\\\\\\\r"))||ix==l?1:0;
}elseif(q[i].t==3){
//Regularexpressioninparens
re=q[i].s;
q[i].ix=[];
q[i].ix[m]=ix;
//Saveindexifneedtoretreat
re.lastIndex=ix;
while(m<q[i].b&&re.test(str_)){
cl=length(RegExp._xxlm);
if(cl>0){
ix+=cl;
m++;
q[i].ix[m]=ix;
}else{
m=q[i].a;
q[i].ix[m-1]=ix;
break;
}
}
if(m==0){
RegExp._xxlm="";
}
if(re._xr>RegExp._xxlp){
RegExp._xxlp=re._xr;
}
RegExp._xxa[Number(re._xr)]=RegExp._xxlm;
}elseif(q[i].t==4){
//Backreferencetoparen
if(RegExp._xp>=(c=Number(q[i].s))){
c=RegExp._xxa[c];
c=this.ignoreCase?c.toLowerCase():c;
cl=length(c);
q[i].ix=[];
q[i].ix[m]=ix;
if(cl>0){
while(m<q[i].b&&ix<l){
if(substring(str,1+ix,cl)==c){
m++;
ix+=cl;
q[i].ix[m]=ix;
}else{
break;
}
}
}else{
m=0;
q[i].a=0;
}
}else{
//Parenisnotready,treatnumberascharcode
c=chr(c);
q[i].ix=[];
q[i].ix[m]=ix;
while(m<q[i].b&&ix<l){
if(substring(str,1+ix,1)==c){
m++;
ix++;
q[i].ix[m]=ix;
}else{
break;
}
}
}
}
q[i].c=m;
if(RegExp.d){
trace(""+m+"matchesfound");
}
}
if(q[i].c<q[i].a){
if(RegExp.d){
trace("notenoughmatches");
}
//Notenoughmatches
if(i>qb){
//Retreatbackanddecrease#ofassumedmatches
i--;
q[i].c--;
if(q[i].c>=0){
ix=(q[i].t==3||q[i].t==4)?q[i].ix[q[i].c]:(q[i].i+q[i].c);
}
if(RegExp.d){
trace("Retreatto#"+i+"c="+q[i].c+"index="+ix);
}
}else{
if(RegExp._xp>1){
//Ifthisisaparen,failingtofindfirstmatchisfatal
break;
}
if(atStart){
//Matchmustbeatthestartofstring/line
if(this.multiline){
//Jumptothebeginningofthenextline
while(ix<=l){
s=substring(str,1+ix++,1);
if(s=="\\\\\\\\\\\\\\\\n"||s=="\\\\\\\\\\\\\\\\r"){
break;
}
}
q[i].c=-10;
}else{
//Nomatch
break;
}
}else{
//Startanewsearchfromnextposition
ix++;
q[i].c=-10;
}
}
}else{
if(RegExp.d){
trace("enoughmatches!");
}
//#ofmatchesok,proceedtonext
i++;
if(i==qc||q[i].t==7){
if(RegExp.d){
trace("Savingbetterresult:r.i=q["+qb+"].i="+q[qb].i);
}
r.i=q[qb].i;
r.li=ix;
break;
}else{
q[i].c=-10;
}
}
}
}
while(i<qc&&q[i].t!=7){
i++;
}
//Jumptothenext"OR"section
}
if(r.i<0){
this.lastIndex=0;
if(RegExp._xp--==1){
RegExp._xxa=[];
RegExp._xxlp=0;
}
returnfalse;
}else{
ix=r.li;
this._xi=r.i;
RegExp._xxlm=substring(str_,r.i+1,ix-r.i);
RegExp._xxlc=substring(str_,1,r.i);
RegExp._xxrc=substring(str_,ix+1,l-ix);
if(ix==r.i){
ix++;
}
this.lastIndex=ix;
if(RegExp._xp--==1){
RegExp.lastMatch=RegExp._xxlm;
RegExp.leftContext=RegExp._xxlc;
RegExp.rightContext=RegExp._xxrc;
RegExp._xaStatic=RegExp._xxa;
RegExp.lastParen=RegExp._xxa[Number(RegExp._xxlp)];
for(i=1;i<10;i++){
RegExp["$"+i]=RegExp._xaStatic[Number(i)];
}
}
returntrue;
}
}
publicfunctionexec(){
varstr=arguments[0]+’’;
if(str==’’){
returnfalse;
}
vart=this.test(str);
if(t){
varra=newArray();
ra.index=this._xi;
ra.input=str;
ra[0]=RegExp.lastMatch;
varl=RegExp._xaStatic.length;
for(vari=1;i<l;i++){
ra[i]=RegExp._xaStatic[Number(i)];
}
}else{
varra=null;
}
returnra;
}
publicstaticfunctionsetStringMethods(){
if(String.prototype.match!=undefined){
return;
}
String.prototype.match=function(){
if(typeof(arguments[0])!="object"){
returnnull;
}
if(arguments[0].const!="RegExp"){
returnnull;
}
varre=arguments[0];
vars=this.valueOf();
varip=0;
varrc=0;
if(re.global){
re.lastIndex=0;
while(re.test(s)){
if(rc==0){
varra=newArray();
}
ra[rc++]=RegExp.lastMatch;
ip=re.lastIndex;
}
re.lastIndex=ip;
}else{
varra=re.exec(s);
rc++;
}
return(rc==0)?null:ra;
};
String.prototype.replace=function(){
if(typeof(arguments[0])!="object"){
returnnull;
}
if(arguments[0].const!="RegExp"){
returnnull;
}
varre=arguments[0];
varrs=arguments[1]+’’;
vars=this;
varr="";
re.lastIndex=0;
if(re.global){
varip=0;
varix=0;
while(re.test(s)){
//Replacebackreferencesinrs
vari=0;
varl=length(rs);
varc="";
varpc="";
varnrs="";
while(i<l){
c=substring(rs,1+i++,1);
if(c=="$"&&pc!="\\"){
c=substring(rs,1+i++,1);
if(isNaN(Number(c))||Number(c)>9){
nrs+="$"+c;
}else{
nrs+=RegExp._xaStatic[Number(c)];
}
}else{
nrs+=c;
}
pc=c;
}
r+=substring(s,ix+1,re._xi-ix)+nrs;
ix=re._xi+length(RegExp.lastMatch);
ip=re.lastIndex;
}
re.lastIndex=ip;
}else{
if(re.test(s)){
r+=RegExp.leftContext+rs;
}
}
r+=re.lastIndex==0?s:RegExp.rightContext;
returnr;
};
String.prototype.search=function(){
if(typeof(arguments[0])!="object"){
returnnull;
}
if(arguments[0].const!="RegExp"){
returnnull;
}
varre=arguments[0];
vars=this;
re.lastIndex=0;
vart=re.test(s);
returnt?re._xi:-1;
};
String.prototype.old_split=String.prototype.split;
String.prototype.split=function(){
if(typeof(arguments[0])=="object"&&arguments[0].const=="RegExp"){
varre=arguments[0];
varlm=arguments[1]==null?9999:Number(arguments[1]);
if(isNaN(lm)){
lm=9999;
}
vars=this;
varra=newArray();
varrc=0;
vargs=re.global;
re.global=true;
re.lastIndex=0;
varip=0;
varipp=0;
varix=0;
while(rc<lm&&re.test(s)){
//trace(re._xi+""+ix+""+RegExp.lastMatch);
if(re._xi!=ix){
ra[rc++]=substring(s,ix+1,re._xi-ix);
}
ix=re._xi+length(RegExp.lastMatch);
ipp=ip;
ip=re.lastIndex;
}
if(rc==lm){
re.lastIndex=ipp;
}else{
re.lastIndex=ip;
}
if(rc==0){
ra[rc]=s;
}else{
if(rc<lm&&length(RegExp.rightContext)>0){
ra[rc++]=RegExp.rightContext;
}
}
re.global=gs;
returnra;
}else{
returnthis.old_split(arguments[0],arguments[1]);
}
};
returntrue;
}
}
范例:
varre=newRegExp("\w{3}","i");
trace(re.test("ABC"));