How to use a variable inside a RegEx pattern?

This question already has an answer here:

  • How do you use a variable in a regular expression? 16 answers

  • 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):

  • How do you use a variable in a regular expression?
  • 链接地址: http://www.djcxy.com/p/76826.html

    上一篇: 如何在javascript中将变量传递给正则表达式

    下一篇: 如何在RegEx模式中使用变量?