Use Regex to insert text to a string - Javascript String Operation

Javascript examples for String Operation:String Convert

Description

Use Regex to insert text to a string

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  w  w  .  ja  v  a  2 s .  com*/
var re = /(function\s+.*\(\)\s+{)(.*)/;
var str = 'function test() {\n//some code...\n}';
var newstr = str.replace(re, '$1\n     \'use strict\';\n$2');
document.write(newstr);
    }

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

Related Tutorials