0
I created a regex ( d{1,}(. d{2})?$ ) that was to allow values in the format 21 or 21.30. I tested the regex on a website and it worked:
I’m making the following code:
const regexMoneyFormat = RegExp('^\d{1,}(\.\d{2})?$');
console.log('valor convertido pra string', price.toString())
if (!regexMoneyFormat.test(price.toString())) {
console.log('resultado do teste', regexMoneyFormat.test(price.toString()))
throw new AppError('Price format invalid, valid shapes: 0 ou 0.00');
}
Only that the values in the format I mentioned, such as the value 1 or the value 60.21 are not passing. From the print I saw that the function toString() is converting correctly, but is not going through the regex.
Why the code isn’t working?