Currently, I am working on code that requires matching a specific number of digits after a decimal point. Here is what I have so far:
var input = getValueFromUser();
var count = getCount();
var x = Number(input.toString().match(/^\d+(?:\.\d{0,1})?/));
alert(x);
The current approach only captures the first digit after the decimal point. However, I would like to dynamically update the regex to match the number based on the value stored in count
. I attempted the following modification:
var pattern = '/^\d+(?:\.\d{0,' + count + '})?/';
var x = Number(input.toString().match(pattern));
Unfortunately, this adjustment always results in 0
for x
.