Use Regular expression to avoid replace part of html tag - Javascript RegExp

Javascript examples for RegExp:Match HTML

Description

Use Regular expression to avoid replace part of html tag

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <style id="compiled-css" type="text/css">

.highlight {/*w  w  w  .j a v  a 2s  .  c  om*/
   color:red;
}
      </style> 
      <script type="text/javascript">
window.onload=function(){
  tag=document.getElementsByTagName('p')[0];
  str=tag.textContent;
  reg=/(l)/gi;
  tag.innerHTML=str.replace(reg,"<i class='highlight'>"+'$1'+"</i>");
}

      </script> 
   </head> 
   <body> 
      <p>
         <i class="highlight">L</i>
         this is a test level
      </p>  
   </body>
</html>

Related Tutorials