I’m trying to use a variable in a RegEx and I’m having problems. This is my function:
const truncateNum = (num, place = 2) => {
const matcher = `/^-?\d+(?:\.\d{0,${place}})?/`;
const re = new RegExp(matcher);
return num.toString().match(re)[0];
};
When running this I get the following error:
Uncaught TypeError: Cannot read property '0' of null
What am I doing wrong here?