具有变量的正则表达式模式 [英] Regular Expression Pattern With A Variable
本文介绍了具有变量的正则表达式模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用正则表达式来检查小数点后的位数。
I am using regular expression to check number of digits after decimal.
当它用于两位或三位数时工作正常,例如 \d {2}
或 \d {3}
,但如果我需要传递一个变量而不是 2
和 3
?
This is working fine when it is used for two or three digits, for example \d{2}
or \d{3}
, but what if I need to pass a variable instead of 2
and 3
?
如何将变量传递给模式?是否有可能?
How do I pass a variable to the pattern? Is it possible?
推荐答案
您可以使用 RegExp ,例如;
//just an example though
var i = 2;
var pattern = new RegExp("\\d{"+i+"}$");
这篇关于具有变量的正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文