RegExp: currency format

I need to format numbers so that thousands are not separated by anything, and hundreds are separated by a dot. for instance

1234.56 12.34 123

I wrote the following ReqExp

amountValue.replace(/^(\d+)[,.](\d{3})[.,](\d{2})$/,'$1' + '$2' +'.'+'$3').replace(/^(\d+),(\d{2})$/,'$1' +'.'+'$2');

      

If there is a way to make it shorter?

Thanks!

+1


a source to share


2 answers


I would just remove any non-digit character, not the decimal point:



amountValue.replace(/[^0-9](?!\d{2}$)/, '').replace(/,(?=\d{2}$)/, '.');

      

+1


a source


I think this might work:

amountValue.replace(/^(\d*)[,.]?(\d{0,3})[.,](\d{2})$/,'$1' + '$2' +'.'+'$3');

      



Try using data.

+1


a source







All Articles