Replace a number ( n ) by repeating a string n times - Javascript String

Javascript examples for String:replace

Description

Replace a number ( n ) by repeating a string n times

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  a2  s.  c  om
var a = '123',
    words = ['foo', 'bar', 'baz'];
a = a.replace(/\d/g, function (b, c, d){
    return words[c].repeat(b);
});
console.log(a);
    }

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

Related Tutorials