日期:2014-05-17  浏览次数:20626 次

判断html标签是否闭合

function TagClose(content){
??? for(var i=0;i<myArray.length;i++){
?????? var reBegin = new RegExp("<"+myArray[i],"ig");
?????? var reEnd = new RegExp("<\/"+myArray[i],"ig");
?????? var begin = 0;
?????? var end = 0;
?????? var begMactches = content.match(reBegin);
?????? var endMactches = content.match(reEnd);
?????? if(begMactches!=null){
?????????? for(var k=0;k<begMactches.length;k++){
????????????? begin++;
?????????? }
?????? }
?????? if(endMactches!=null){
?????????? for(var j=0;j<endMactches.length;j++){
????????????? end++;
?????????? }
?????? }
?????? if(begin!=end){
?????????? alert("代码中含有未闭合的【"+myArray[i]+"】标签,请检查后再进行插入");
?????????? return false;
?????? }
??? }
??? begMactches = content.match(new RegExp("<\!\-\-","ig"));
??? endMactches = content.match(new RegExp("\-\->","ig"));
??? var a = 0;
??? var b = 0;
??? if(begMactches!=null){
??????? //alert(begMactches.length);
??????? for(var n=0;n<begMactches.length;n++){
?????????? a++;
??????? }
??? }
??? if(endMactches!=null){
?????? for(var m=0;m<endMactches.length;m++){
????????? b++;
?????? }
??? }
??? if(a!=b){
?????? alert("代码中含有未闭合的【<!-- -->】标签,请检查后再进行插入");
?????? return false;
??? }
??? return true;
}