Reputation: 13
var precision = !!values.amountprecision && values.amountprecision || '2';
var temp = /^\d+(\.\d{1,2})?$/;
temp.test(1.1221);
In the above expression, i have explicitly mentioned 1 to 2 values after decimal points. but i want to set the digits after decimal point should be based on the precision variable.
Upvotes: 0
Views: 53
Reputation: 46
According your code 「var temp = /^\d+(.\d{1,2})?$/;」, I guess you need an integer or a float with 1 or 2 digits after the decimal point. And the number of digits determined by a variable, precision. If so, try this
let temp = new RegExp(`^\\d+(\\.\\d{${precision}})?$`);
Upvotes: 0
Reputation: 198324
Use the excplicit constructor instead of the literal:
var temp = new RegExp('^\d+(\.\d{' + precision + '})?$');
Upvotes: 1