Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带RegExp的正则表达式javascript_Javascript_Regex_Regular Language - Fatal编程技术网

带RegExp的正则表达式javascript

带RegExp的正则表达式javascript,javascript,regex,regular-language,Javascript,Regex,Regular Language,我继续读下去 我看到了两种创建新RegExp的方法:/ab+c/I; 新RegExp('ab+c','i'); 新的RegExp(/ab+c/,'i') 但我想创建新的RegExp,如下所示: var re = new RegExp(`\d+${variable}`, 'g'); 我试过了,但没用。我该怎么做呢?用\在中逃逸,例如逃逸: \d,写入\\d \w,写\\w \s,写入\\s ……等等 let变量=1; const re=new RegExp(`\\d+${variable}

我继续读下去

我看到了两种创建新RegExp的方法:
/ab+c/I;
新RegExp('ab+c','i');
新的RegExp(/ab+c/,'i')

但我想创建新的RegExp,如下所示:

var re = new RegExp(`\d+${variable}`, 'g');
我试过了,但没用。我该怎么做呢?

\
在中逃逸,例如逃逸:

  • \d
    ,写入
    \\d
  • \w
    ,写
    \\w
  • \s
    ,写入
    \\s
……等等

let变量=1;
const re=new RegExp(`\\d+${variable}`,'g');

控制台日志(re)您可以将整个表达式编写为字符串(您可以将变量的值连接到该字符串),然后使用
eval()
将其更改为工作javascript表达式:

var x = "A";
var re = eval("new RegExp('\d+$" + x + "', 'g')");

请留下变量中的样本。