去掉html标签
1 2 3
| function removeHtmlTab(tab) { return tab.replace(/<[^<>]+?>/g,''); }
|
普通字符转换成转意符
1 2 3
| function html2Escape(sHtml) { return sHtml.replace(/[<>&"]/g,function(c){return {'<':'<','>':'>','&':'&','"':'"'}[c];}); }
|
转意符换成普通字符
1 2 3 4
| 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];}); }
|
转成空格
1 2 3 4
| function nbsp2Space(str) { var arrEntities = {'nbsp' : ' '}; return str.replace(/&(nbsp);/ig, function(all, t){return arrEntities[t]}) }
|
回车转为br标签
1 2 3
| function return2Br(str) { return str.replace(/\r?\n/g,"<br />"); }
|
去除开头结尾换行
1 2 3 4 5 6
| function trimBr(str) { str=str.replace(/((\s| )*\r?\n){3,}/g,"\r\n\r\n"); str=str.replace(/^((\s| )*\r?\n)+/g,''); str=str.replace(/((\s| )*\r?\n)+$/g,''); return str; }
|
将多个连续空格合并成一个空格
1 2 3 4
| function mergeSpace(str) { str=str.replace(/(\s| )+/g,' '); return str; }
|
转载自 http://www.cnblogs.com/leejersey/p/4568092.html