Home >Web Front-end >JS Tutorial >Sample code sharing for JavaScript regular replacement of HTML tags (picture)

Sample code sharing for JavaScript regular replacement of HTML tags (picture)

黄舟
黄舟Original
2017-03-24 14:54:451403browse

This article mainly introducesJavaScriptRegular replacementHTML tagFunction, combined with a complete example form, a detailed analysis of JavaScript regular replacement string operation related implementation skills, friends in need can refer to the following

This article describes the JavaScript regular replacement HTML tag function Share. For your reference, the details are as follows:

1. Description

Get the HTML string (including tags) and replace it with regular expression HTML tag, outputs the string after replacing

2. Implement JavaScript code

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

3. Running result

(1) During initialization

(2) After clicking the button

(3) After clicking "OK"

4. Complete example:

<!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>

The above is the detailed content of Sample code sharing for JavaScript regular replacement of HTML tags (picture). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn