/*
* JS正则表达式类
* 例:
* var str="<html><script type='text/javascript'>var a=10;</script><body><script type='text/javascript'>var b=10;</script><a href='javascript:;'/>aaaa< /a ><img src='abc.jpg' id='abcd' alt='abc你好' /><a href='index.php'>你好</a></body></html>";
* var reg=new RegExps();
* var s=reg.GetTag(str,'img');
* alert(reg.GetAttrib(s[0],'alt'));
* alert(reg.Is('.*',"b"));
* var as=reg.Get(str,"<script type='text/javascript'>","</script>");
* alert(reg.GetTagName(str,new Array('html','body','script'),0));
* alert(reg.getElememtById(str,'abcd'));
JS正则表达式类=================================
* 方法列表 *
==============================================
* GetTag(string,tag); 反回 数组 *
==============================================
* GetTagName(str,new Array()[,0]);反回字符串*
==============================================
* getElememtBy(string,name,value);反回字符串*
==============================================
* getElememtById(string,id); 反回 字符串*
==============================================
* getElememtByName(string,name); 反回 字符串*
==============================================
* GetAttrib(string,Attrib); 反回 字符串*
==============================================
* Get(string,start,end); 反回 数组 *
==============================================
* GetText(string); 反回 字符串 *
==============================================
* ClsHtml(string); 反回 字符串 *
==============================================
* ClsScript(string); 反回 字符串 *
==============================================
* ClsCss(string); 反回 字符串 *
==============================================
* FindString(string,str);反回 布尔型 *
==============================================
* Is(str,string); 反回 布尔型 *
==============================================
* Trim(string|Array); 反回 字符串 | 数组 *
==============================================
* Delete(string,value); 反回 字符串 *
==============================================
*/
function RegExps(){
this.GetTag=function(string,tag){
if(tag.toLowerCase()=='img')
var regexp=new RegExp("\<"+tag+"((.|\r\n)*?)\>","gi");
else
var regexp=new RegExp("\<"+tag+"((.|\r\n)*?)\>((.|\r\n)*?)\<((.|\r\n)*?)\/"+tag+"((.|\r\n)*?)\>","gi");
var tags= string.match(regexp);
if(tags==null)
return new Array("");
else
return tags;
}
this.GetTagName=function(string,name,id){
if(string==null || name==null)return false;
var strings=new Array(string);
if(typeof(name)=="object"){
for(var i=0;i<name.length;i++){
strings.length=0;
strings=this.GetTag(string,name[i]);
if(strings==null)return "";
string=strings[0];
}
if(id==null)
return strings;
else
return strings[id];
}
else{
strings=this.GetTag(string,name);
if(id==null)
return strings;
else
return strings[id];
}
}
this.getElememtBy=function(string,name,value){
if(name==null || string==null || value==null)return false;
var regexp=new RegExp("(<([^>]*?) "+name+"=('| |\")"+value+"('| |\")((.|\r\n)*?)>)","gi");
var strings=string.match(regexp);
if(strings!=null){
var tagObject=strings[0];
regexp=new RegExp("[^< ]* ","gi");
strings=strings[0].match(regexp);
if(strings==null)
return null;
else{
strings[0]=strings[0].replace(/^( | )*/,'');
strings[0]=strings[0].replace(/( | )*$/,'');
if(strings[0].toLowerCase()=='img'){
strings[0]=tagObject;
}
else{
regexp=new RegExp("(<([^>]*?) "+name+"=('| |\")"+value+"('| |\")((.|\r\n)*?)>((.|\r\n)*?)<((.|\r\n)*?)/"+strings[0]+"((.|\r\n)*?)>)","gi");
strings=string.match(regexp);
}
if(strings==null)
return null;
else
return strings[0];
}
}
}
this.getElememtById=function(string,id){
if(id==null || string==null)return false;
var regexp=new RegExp("(<([^>]*?) id=('| |\")"+id+"('| |\")((.|\r\n)*?)>)","gi");
var strings=string.match(regexp);
if(strings!=null){
var tagObject=strings[0];
regexp=new RegExp("[^< ]* ","gi");
strings=strings[0].match(regexp);
if(strings==null)
return null;
else{
strings[0]=strings[0].replace(/^( | )*/,'');
strings[0]=strings[0].replace(/( | )*$/,'');
if(strings[0].toLowerCase()=='img'){
strings[0]=tagObject;
}
else{
regexp=new RegExp("(<([^>]*?) id=('| |\")"+id+"('| |\")((.|\r\n)*?)>((.|\r\n)*?)<((.|\r\n)*?)/"+strings[0]+"((.|\r\n)*?)>)","gi");
strings=string.match(regexp);
}
if(strings==null)
return null;
else
return strings[0];
}
}
}
this.getElememtByName=function(string,name){
if(name==null || string==null)return false;
var regexp=new RegExp("(<([^>]*?) name=('| |\")"+name+"('| |\")((.|\r\n)*?)>)","gi");
var strings=string.match(regexp);
if(strings!=null){
var tagObject=strings[0];
regexp=new RegExp("[^< ]* ","gi");
strings=strings[0].match(regexp);
if(strings==null)
return null;
else{
strings[0]=strings[0].replace(/^( | )*/,'');
strings[0]=strings[0].replace(/( | )*$/,'');
if(strings[0].toLowerCase()=='img'){
strings[0]=tagObject;
}
else{
regexp=new RegExp("(<([^>]*?) name=('| |\")"+name+"('| |\")((.|\r\n)*?)>((.|\r\n)*?)<((.|\r\n)*?)/"+strings[0]+"((.|\r\n)*?)>)","gi");
strings=string.match(regexp);
}
if(strings==null)
return null;
else
return strings[0];
}
}
}
this.GetAttrib=function(string,Attrib){
var regexp=new RegExp(" "+Attrib+"=((.|\r\n)*?)( |\>|/\>)","gi");
var Attribs=string.match(regexp);
if(Attribs!=null){
regexp=new RegExp("(^ "+Attrib+"=)","gi");
Attribs[0]=Attribs[0].replace(regexp,'');
Attribs[0]=Attribs[0].replace(/( | |'|"| >| >|'>|">| \/>| \/>|'\/>|"\/>)*$/,'');
Attribs[0]=Attribs[0].replace(/^( | |'|")*/,'');
return Attribs[0];
}
}
this.Get=function(string,start,end){
var regexp=new RegExp(start+"((.|\r\n)*?)"+end,"g");
var strings=string.match(regexp);
var objs=new Array();
objs[0]=new Array();
objs[1]=new Array();
if(strings!=null){
for(var i=0;i<strings.length;i++){
var regexp2=new RegExp("(^"+start+"|"+end+"$)","g");
objs[1][i] = strings[i].replace(regexp2,'');
objs[0][i] = strings[i];
}
return objs;
}
}
this.FindString=function(string,str){
if(id!=null)
var regexp=new RegExp("((.|\r\n)*?)"+str+"((.|\r\n)*?)","gi");
else
var regexp=new RegExp("((.|\r\n)*?)"+str+"((.|\r\n)*?)");
if(string.search(regexp)==0)
return true;
else
return false;
}
this.Is=function(str,string){
var regexp=new RegExp(str);
return regexp.test(string);
}
this.GetText=function(string){
string=this.ClsScript(string);
string=this.ClsCss(string);
string=this.ClsHtml(string);
return string;
}
this.ClsHtml=function(string){
return string.replace(/(\<((.|\r\n)*?)\>)/gi,'');
}
this.ClsScript=function(string){
return string.replace(/\<script ((.|\r\n)*?)\>((.|\r\n)*?)\<((.|\r\n)*?)\/script((.|\r\n)*?)\>/gi,'');
}
this.ClsCss=function(string){
return string.replace(/\<style ((.|\r\n)*?)\>((.|\r\n)*?)\<((.|\r\n)*?)\/style((.|\r\n)*?)\>/gi,'');
}
this.Trim=function(strings){
if(typeof(strings)=="object"){
for(var i=0;i<strings.length;i++){
if(strings[i]=="" || strings[i]==null){
strings[i]=strings[strings.length-1];
strings.length=strings.length-1;
}
else{
if(typeof(strings[i])=="object")strings[i]=this.Trim(strings[i]);
}
}