我希望将int值替换为正则表达式:
const decimal = 16;
const amount = 100;
if (amount.match(/^\d{1,}(\.\d{0,8})?$/)) // how can I replace 8 with decimal value 16
如何用十进制值替换8?
RegExp
构造函数接受一个字符串以从中创建模式。您可以根据需要构造字符串:
我认为您可能要检查此const decimal = 16;
const amount = 100;
const regexp = new RegExp('^\\d{1,}(\\.\\d{0,' + decimal + '})?$');
if (amount.toString().match(regexp)) // do further work