Formatting numbers with regex - Javascript RegExp

Javascript examples for RegExp:Match Number

Description

Formatting numbers with regex

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  va2 s .  co  m*/
function format2(n, currency) {
    return currency + " " + n.toFixed(2).replace(/^\s*(?:[1-9]\d{0,2}(?:\.\d{3})*|0)(?:,\d{1,2})?$/g, "$1,");
}
console.log(format2(1, 'R$'));
    }

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

Related Tutorials