How to use a variable inside a RegEx pattern?
This question already has an answer here:
Use new RegExp(string)
to build a regular expression dynamically. The literal /../
form cannot be used with dynamic content.
Make sure to have a valid pattern after building the string.
var len = 99;
var re = new RegExp(".(?=.{" + len + "})", "g");
var output = input.replace(re, "*")
Also see (and vote for dupe of):
上一篇: 如何在javascript中将变量传递给正则表达式
下一篇: 如何在RegEx模式中使用变量?