0

I have a regex:

const reg = /^-?[0-9]*\.?[0-9]*$/;

Which checks for numbers with decimal places but I want to match commas too. It should match given below number comma should be before three digits, for example:

'12.00000'
'112.00000'
'1,123.00000'
'12,123.00000'
'123,123.00000'

To match decimal numbers:

const reg1 = /^-?[0-9]*\.?[0-9]*$/

To match commas:

const reg2 = /^\d{1,3}(?:,\d{3})*$/

I tried to combine both and tried to match:

  const reg1= new RegExp(/^-?[0-9]*\.?[0-9]*$/);
  const reg2= new RegExp(/^\d{1,3}(?:,\d{3})*$/);
  const finalReg = new RegExp(reg1.source + '|' + reg2.source);

But It does not match the number with commas and decimal, Is there any other way? Please advice.

Ans
  • 153
  • 1
  • 13

0 Answers0