How to add comma as number separator in user input?

I found this code and works well to add a decimal as user types. I actually need a comma to be added as user types, not decimal.

$w . onReady ( function (){ $w ( “#input3” ). onKeyPress (( event ) => { setTimeout ( parseString , 10 );}) function parseString (){ let value = $w ( “#input3” ). value ; let stringDigits = value . replace ( /[^0-9]/g , “” ); let finalString = stringDigits . replace ( /…\B/g , “$&.” ); $w ( “#input3” ). value = finalString ;}})

Anyone know what to change in this code to add comma?
Ex: 1,000/10,000/100,000/1,000,000/10,000,000 etc.

Appreciate any help.

If you input is string X then this code below will do it. Just make sure it is in the onKeyPress event.

x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")