JS Html轉(zhuǎn)義和反轉(zhuǎn)義(html編碼和解碼)的實(shí)現(xiàn)與使用方法總結(jié)
本文實(shí)例講述了JS Html轉(zhuǎn)義和反轉(zhuǎn)義(html編碼和解碼)的實(shí)現(xiàn)與使用方法。分享給大家供大家參考,具體如下:
1、JS實(shí)現(xiàn)html轉(zhuǎn)義和反轉(zhuǎn)義主要有兩種方式:1)、利用用瀏覽器內(nèi)部轉(zhuǎn)換器實(shí)現(xiàn)html轉(zhuǎn)義;
2)、用正則表達(dá)式實(shí)現(xiàn)html轉(zhuǎn)義;
2、封裝的JS工具類:var HtmlUtil = { /*1.用瀏覽器內(nèi)部轉(zhuǎn)換器實(shí)現(xiàn)html編碼(轉(zhuǎn)義)*/ htmlEncode:function (html){ //1.首先動(dòng)態(tài)創(chuàng)建一個(gè)容器標(biāo)簽元素,如DIV var temp = document.createElement ('div'); //2.然后將要轉(zhuǎn)換的字符串設(shè)置為這個(gè)元素的innerText或者textContent (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html); //3.最后返回這個(gè)元素的innerHTML,即得到經(jīng)過(guò)HTML編碼轉(zhuǎn)換的字符串了 var output = temp.innerHTML; temp = null; return output; }, /*2.用瀏覽器內(nèi)部轉(zhuǎn)換器實(shí)現(xiàn)html解碼(反轉(zhuǎn)義)*/ htmlDecode:function (text){ //1.首先動(dòng)態(tài)創(chuàng)建一個(gè)容器標(biāo)簽元素,如DIV var temp = document.createElement('div'); //2.然后將要轉(zhuǎn)換的字符串設(shè)置為這個(gè)元素的innerHTML(ie,火狐,google都支持) temp.innerHTML = text; //3.最后返回這個(gè)元素的innerText或者textContent,即得到經(jīng)過(guò)HTML解碼的字符串了。 var output = temp.innerText || temp.textContent; temp = null; return output; }, /*3.用正則表達(dá)式實(shí)現(xiàn)html編碼(轉(zhuǎn)義)*/ htmlEncodeByRegExp:function (str){var temp = ''; if(str.length == 0) return ''; temp = str.replace(/&/g,'&'); temp = temp.replace(/</g,'<'); temp = temp.replace(/>/g,'>'); temp = temp.replace(/s/g,' '); temp = temp.replace(/’/g,'''); temp = temp.replace(/'/g,'"'); return temp; }, /*4.用正則表達(dá)式實(shí)現(xiàn)html解碼(反轉(zhuǎn)義)*/ htmlDecodeByRegExp:function (str){var temp = ''; if(str.length == 0) return ''; temp = str.replace(/&/g,'&'); temp = temp.replace(/</g,'<'); temp = temp.replace(/>/g,'>'); temp = temp.replace(/ /g,' '); temp = temp.replace(/'/g,'’'); temp = temp.replace(/"/g,'''); return temp; }, /*5.用正則表達(dá)式實(shí)現(xiàn)html編碼(轉(zhuǎn)義)(另一種寫(xiě)法)*/ html2Escape:function(sHtml) { return sHtml.replace(/[<>&']/g,function(c){return {’<’:’<’,’>’:’>’,’&’:’&’,’'’:’"’}[c];}); }, /*6.用正則表達(dá)式實(shí)現(xiàn)html解碼(反轉(zhuǎn)義)(另一種寫(xiě)法)*/ escape2Html:function (str) { var arrEntities={’lt’:’<’,’gt’:’>’,’nbsp’:’ ’,’amp’:’&’,’quot’:’'’}; return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];}); } };3、測(cè)試及效果:
1)、html代碼:
<div>&</div><div>&</div><div id='testdiv'></div><div id='testdiv1'></div><div id='testdiv2'></div><div id='regdiv'></div><div id='regdiv1'></div><div id='regdiv2'></div><div id='regdiv3'></div><div id='regdiv4'></div><div id='regdiv5'></div>
2)、js測(cè)試代碼:
var strHtml=’<div style='color:blue'>符號(hào):&<div>’; document.getElementById('testdiv').innerHTML=strHtml; var encodedHtml= HtmlUtil.htmlEncode(strHtml);// '<div style='color:blue'>符號(hào):&amp;<div>' document.getElementById('testdiv1').innerHTML=encodedHtml; var decodedHtml=HtmlUtil.htmlDecode(encodedHtml);// ’<div style='color:blue'>符號(hào):&<div>’ document.getElementById('testdiv2').innerHTML=decodedHtml; var strHtml_1=’<div style='color:red'>符號(hào):&<div>’; document.getElementById('regdiv').innerHTML=strHtml_1; var encodedHtml_1 =HtmlUtil.htmlEncodeByRegExp(strHtml_1);// '<div style='color:red'>符號(hào):&amp;<div>' document.getElementById('regdiv1').innerHTML=encodedHtml_1; var decodedHtml_1 =HtmlUtil.htmlDecodeByRegExp(encodedHtml_1);// ’<div style='color:blue'>符號(hào):&<div>’ document.getElementById('regdiv2').innerHTML=decodedHtml_1; var strHtml_2=’<div style='color:green'>符號(hào):&<div>’; document.getElementById('regdiv3').innerHTML=strHtml_2; var encodedHtml_2 =HtmlUtil.htmlEncodeByRegExp(strHtml_2);// '<div style='color:green'>符號(hào):&amp;<div>' document.getElementById('regdiv4').innerHTML=encodedHtml_2; var decodedHtml_2 =HtmlUtil.htmlDecodeByRegExp(encodedHtml_2);// ’<div style='color:green'>符號(hào):&<div>’ document.getElementById('regdiv5').innerHTML=decodedHtml_2;
3)、效果圖:
1)、去掉字符串中的html標(biāo)簽
function removeHtmlTab(tab) {return tab.replace(/<[^<>]+?>/g,’’);//刪除所有HTML標(biāo)簽}removeHtmlTab(’<div id='test'>zyl</div><span>zzc</span>’);// zylzzc
2)、回車(chē)rn轉(zhuǎn)為<br/>標(biāo)簽
function return2Br(str) { return str.replace(/r?n/g,'<br />');}
3)、去除開(kāi)頭結(jié)尾換行,并將連續(xù)3次以上換行轉(zhuǎn)換成2次換行
function trimBr(str) { str=str.replace(/((s| )*r?n){3,}/g,'rnrn');//限制最多2次換行 str=str.replace(/^((s| )*r?n)+/g,’’);//清除開(kāi)頭換行 str=str.replace(/((s| )*r?n)+$/g,’’);//清除結(jié)尾換行 return str;}
4)、將多個(gè)連續(xù)空格合并成一個(gè)空格
function mergeSpace(str) { str=str.replace(/(s| )+/g,’ ’); return str;}
PS:這里再為大家提供幾款相關(guān)工具供大家參考使用:
在線HTML轉(zhuǎn)義/反轉(zhuǎn)義工具:http://tools.jb51.net/transcoding/html_transcode
Native/Unicode在線編碼轉(zhuǎn)換工具:http://tools.jb51.net/transcoding/native2unicode
在線中文漢字/ASCII碼/Unicode編碼互相轉(zhuǎn)換工具:http://tools.jb51.net/transcoding/chinese2unicode
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《javascript編碼操作技巧總結(jié)》、《JavaScript加密解密技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. python爬蟲(chóng)beautifulsoup解析html方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python 實(shí)現(xiàn)aes256加密4. 詳解Python模塊化編程與裝飾器5. css進(jìn)階學(xué)習(xí) 選擇符6. Python性能測(cè)試工具Locust安裝及使用7. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式8. 使用Python解析Chrome瀏覽器書(shū)簽的示例9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python web框架的總結(jié)
