Blogger Information
Blog 16
fans 0
comment 0
visits 18217
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript转换HTML转义符
忧郁之子的博客
Original
988 people have browsed it

//去掉html标签 

function removeHtmlTab(tab) {
 return tab.replace(/<[^<>]+?>/g,'');//删除所有HTML标签
}

//普通字符转换成转意符   

function html2Escape(sHtml) {
 return sHtml.replace(/[<>&"]/g,function(c){return {'<':'&lt;','>':'&gt;','&':'&amp;','"':'&quot;'}[c];});
}

//转意符换成普通字符   

function escape2Html(str) {
 var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
 return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
}

// &nbsp;转成空格   

function nbsp2Space(str) {
 var arrEntities = {'nbsp' : ' '};
 return str.replace(/&(nbsp);/ig, function(all, t){return arrEntities[t]})
}

//回车转为br标签   

function return2Br(str) {
 return str.replace(/\r?\n/g,"<br />");
}

  //去除开头结尾换行,并将连续3次以上换行转换成2次换行 

function trimBr(str) {
 str=str.replace(/((\s|&nbsp;)*\r?\n){3,}/g,"\r\n\r\n");//限制最多2次换行
 str=str.replace(/^((\s|&nbsp;)*\r?\n)+/g,'');//清除开头换行
 str=str.replace(/((\s|&nbsp;)*\r?\n)+$/g,'');//清除结尾换行
 return str;
}

// 将多个连续空格合并成一个空格   

function mergeSpace(str) {
 str=str.replace(/(\s|&nbsp;)+/g,' ');
 return str;
}
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post