jQuery Replacing dom Element Tag Usage Example with Regular Expressions

  • 2021-07-10 18:54:06
  • OfStack

This article illustrates how jQuery uses regular expressions to replace dom element tags. Share it for your reference, as follows:

Here, the tags in dom elements are mainly replaced by the following regular expressions:

/ < [\/]?(div)([^ < > ]*) > /g

The specific example code is as follows:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="jquery.min.js"></script>
</head>
<body>
<div id="abc">
  <div style="text-align: center;">
    test
  </div>
</div>
</body>
<script>
  var oriTest = $("#abc").html();
  var result = oriTest.replace(/<[\/]?(div)([^<>]*)>/g, function (m, m1) {
    console.log(m);
    return m.replace('div', 'p');
  });
  $("#abc").html(result);
</script>
</html>

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

For more information about jQuery, please see the topics on this site: "jQuery Regular Expression Usage Summary", "jQuery String Operation Skills Summary", "jQuery Operation xml Skills Summary", "jQuery Extension Skills Summary", "jquery Selector Usage Summary" and "jQuery Common Plug-ins and Usage Summary"

I hope this article is helpful to everyone's jQuery programming.


Related articles: