如何在RegEx模式中使用int变量?

问题描述 投票:-1回答:2

我希望将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?

javascript
2个回答
2
投票

RegExp构造函数接受一个字符串以从中创建模式。您可以根据需要构造字符串:


-1
投票

我认为您可能要检查此const decimal = 16; const amount = 100; const regexp = new RegExp('^\\d{1,}(\\.\\d{0,' + decimal + '})?$'); if (amount.toString().match(regexp)) // do further work

© www.soinside.com 2019 - 2024. All rights reserved.