溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JavaScript中正則如何替換HTML標簽功能

發(fā)布時間:2021-07-09 15:15:50 來源:億速云 閱讀:1139 作者:小新 欄目:web開發(fā)

這篇文章主要介紹JavaScript中正則如何替換HTML標簽功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

具體如下:

1、說明

獲取HTML字符串(包含標簽),通過正則表達式替換HTML標簽,輸出替換后的字符串

2、實現(xiàn)JavaScript代碼

function deleteTag()
{
  var regx = /<[^>]*>|<\/[^>]*>/gm;
  var tagStr = $("#ul_li").html();
  alert("替換之前的字符串:" + tagStr);
  var result = tagStr.replace(regx,"");
  alert("替換之后的字符串:" + result);
}

3、運行結果

(1)初始化時

JavaScript中正則如何替換HTML標簽功能

(2)單擊按鈕后

JavaScript中正則如何替換HTML標簽功能

(3)單擊“確定”后

JavaScript中正則如何替換HTML標簽功能

4、完整實例:

<!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標簽</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標簽" onclick="deleteTag()"/>
 </body>
</html>

以上是“JavaScript中正則如何替換HTML標簽功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI