Using Regex with global attribute - Javascript RegExp

Javascript examples for RegExp:Property

Description

Using Regex with global attribute

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() {//w w w. j  a  va 2s . c o  m
var o = { bob : 'test is cool', steve : 'is not test test' };
retString = '[BOB] [STEVE] [BOB]';
for (n in o) {
  pattern = new RegExp('\\[' + n.toUpperCase() + '\\]', 'g');
  retString = retString.replace(pattern, o[n].toString());
}
console.log(retString);
    });

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

Related Tutorials