Javascript examples for String:replace
Replace a number ( n ) by repeating a string n times
<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>