Javascript examples for RegExp:Match HTML
Use Regular expression to avoid replace part of html tag
<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>