js addDqmForPP给标签内属性值加上双引号的函数
实现代码一:
给标签内属性值加上双引号
varaddDqmForPP=function(shtml){
returnshtml.replace(/([^\=]*\=)(\s?[^\"\s\>]*)/ig,function(a,b,c,d,e){return(c)?(newRegExp("<[^>]*"+c.replace(/(\^|\(|\)|\[|\]|\{|\}|\?|\-|\\|\/|\||\$)/g,'\\$1')+"[^>]*>","i").test(e))?b+'"'+c+'"':b+c:b});
};
完整测试代码
<textareaid="t"style="height:200px;width:300px;">
<ULclass=list>
<LIclass=feed_item><SPANclass=note>01/12/2008</SPAN>
<P><Atitle=GE公司推出“汪力和李邦”网络广告href=http://www.genewscenter.com/{$content}\Detail.asp?ReleaseID=4886&NewsAreaID=2&changeCurrentLocale=5_eventID="4">GE公司推出“汪力和李邦”网络广告</A></P></LI>
<LIclass=feed_item><SPANclass=note>26/11/2008</SPAN>
<P><Atitle=GE石油天然气集团在中国创造新的快速安装记录href="http://www.genewscenter.com/content/Detail.asp?ReleaseID=4879&NewsAreaID=2&changeCurrentLocale=5"_eventID="5">GE石油天然气集团在中国创造新的快速安装记录</A></P></LI></UL>
</textarea>
<scripttype="text/javascript">
varsa='<ahref=index.html>hello</a><inputonlick=a();typle="submit"/><inputonlick=a();value=oktyple="submit"/><span>a=8+4</span>';
varsb=document.getElementById('t').value;
varaddDqmForPP=function(shtml){
returnshtml.replace(/([^\=]*\=)(\s?[^\"\s\>]*)/ig,function(a,b,c,d,e){return(c)?(newRegExp("<[^>]*"+c.replace(/(\^|\(|\)|\[|\]|\{|\}|\?|\-|\\|\/|\||\$)/g,'\\$1')+"[^>]*>","i").test(e))?b+'"'+c+'"':b+c:b});
};
alert(addDqmForPP(sa));
alert(addDqmForPP(sb));
</script>
实现代码二:
给标签内属性值加上双引号标记变小写
核心代码
varpartialXHTML=function(value){
returnvalue.replace(/<(\/?[a-zA-Z]+\s*)((?:"[^"]*"|'[^']*'|(?!'|"|\/?>).)*)(\/?>)/g,function(l,$1,$2,$3){
return'<'+$1.toLowerCase()+$2.replace(/([^=]+)=(?:(")[^"]*"|(')[^']*'|((?:(?!\s|'|"|\/?>).)*))/g,function(l,$1,$2,$3,$4){
return($2||$3)?l:$1+'="'+$4+'"';
})+$3;
});
};
完整代码
<textareastyle="height:200px;width:300px;">
<ULclass=list>
<LIclass=feed_item><SPANclass=note>01/12/2008</SPAN>
<P><Atitle=GE公司推出“汪力和李邦”网络广告href="http://www.genewscenter.com/content/Detail.asp?ReleaseID=4886&NewsAreaID=2&changeCurrentLocale=5"_eventID="4">GE公司推出“汪力和李邦”网络广告</A></P></LI>
<LIclass=feed_item><SPANclass=note>26/11/2008</SPAN>
<P><Atitle=GE石油天然气集团在中国创造新的快速安装记录href="http://www.genewscenter.com/content/Detail.asp?ReleaseID=4879&NewsAreaID=2&changeCurrentLocale=5"_eventID="5">GE石油天然气集团在中国创造新的快速安装记录</A></P></LI></UL>
</textarea>
<scripttype="text/javascript">
varpartialXHTML=function(value){
returnvalue.replace(/<(\/?[a-zA-Z]+\s*)((?:"[^"]*"|'[^']*'|(?!'|"|\/?>).)*)(\/?>)/g,function(l,$1,$2,$3){
return'<'+$1.toLowerCase()+$2.replace(/([^=]+)=(?:(")[^"]*"|(')[^']*'|((?:(?!\s|'|"|\/?>).)*))/g,function(l,$1,$2,$3,$4){
return($2||$3)?l:$1+'="'+$4+'"';
})+$3;
});
};
varparse=function(t){
t.value=partialXHTML(t.value);
};
</script>
<inputtype="button"value="修正"onclick="parse(document.getElementsByTagName('textarea')[0])"/>