本文實(shí)例講述了JavaScript正則替換HTML標(biāo)簽功能。分享給大家供大家參考,具體如下:
1、說明
獲取HTML字符串(包含標(biāo)簽),通過正則表達(dá)式替換HTML標(biāo)簽,輸出替換后的字符串
2、實(shí)現(xiàn)JavaScript代碼
function deleteTag(){ var regx = /<[^>]*>|<//[^>]*>/gm; var tagStr = $("#ul_li").html(); alert("替換之前的字符串:" + tagStr); var result = tagStr.replace(regx,""); alert("替換之后的字符串:" + result);}
3、運(yùn)行結(jié)果
(1)初始化時(shí)
(2)單擊按鈕后
(3)單擊“確定”后
4、完整實(shí)例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JavaScript替換HTML標(biāo)簽</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript"> function deleteTag() { var regx = /<[^>]*>|<//[^>]*>/gm; var tagStr = $("#ul_li").html(); alert("替換之前的字符串:" + tagStr); var result = tagStr.replace(regx,""); alert("替換之后的字符串:" + result); } </script> </head> <body> <ul id="ul_li"> <li class="peach">桃樹</li> <li class="pear">梨樹</li> <li>樟樹</li> <li>楓樹</li> <li>松樹</li> </ul> <input type="button" value="替換HTML標(biāo)簽" onclick="deleteTag()"/> </body></html>
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測(cè)試工具:
http://tools.VeVB.COm/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.VeVB.COm/regex/create_reg
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript正則表達(dá)式技巧大全》、《JavaScript替換操作技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注