Use regex to Replace word on the regex search result - Javascript String Operation

Javascript examples for String Operation:String Replace

Description

Use regex to Replace word on the regex search result

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  . j  a v  a  2s . co m*/
a = 'item a',
b = 'item b',
c = 'item c'
string = '@a@ and @b@@c@ this is a test!',
regex = /@(\w+?)@/g;

console.log(string.replace(regex, function (match, group) {
    return window[group];
}));
    }

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

Related Tutorials