Use Regular expressions in javascript and replace html tag - Javascript RegExp

Javascript examples for RegExp:Match HTML

Description

Use Regular expressions in javascript and replace html tag

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from  w  ww  .jav  a 2s .co  m
var text = 'Some text...<div class=example><pre><ul><li>Item</li></ul></pre><div class=showExample></div></div>Some text...';
console.log(text);
match = text.match(/<pre>(.*?)<\/pre>/)[1];

match = match.replace(/</g, '&lt;').replace(/>/g, '&gt;');

text = text.replace(/<pre>.*?<\/pre>/, '<pre>'+match+'</pre>');

console.log(text);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials