Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.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
Javascript 如何更改我的正则表达式以匹配多个承诺语法?_Javascript_Regex - Fatal编程技术网

Javascript 如何更改我的正则表达式以匹配多个承诺语法?

Javascript 如何更改我的正则表达式以匹配多个承诺语法?,javascript,regex,Javascript,Regex,我有一个正则表达式,它符合承诺代码的语法 正则表达式也在promise语法中匹配(按组名:a、b、c和d) 因此,使用正则表达式,我知道回调中有什么,参数是什么 then\([\n\s]+function\s*\((?<a>.*)\)\s*\{[\n\s]+(?<b>[\s\S]+)\},[\n\s]*function\s*\((?<c>.*)\)\s*\{[\n\s]*(?<d>[\s\S]+)?\}[\n\s]*\); 老实说,在使用Java

我有一个正则表达式,它符合承诺代码的语法

正则表达式也在promise语法中匹配(按组名:a、b、c和d)

因此,使用正则表达式,我知道回调中有什么,参数是什么

then\([\n\s]+function\s*\((?<a>.*)\)\s*\{[\n\s]+(?<b>[\s\S]+)\},[\n\s]*function\s*\((?<c>.*)\)\s*\{[\n\s]*(?<d>[\s\S]+)?\}[\n\s]*\);

老实说,在使用JavaScript的正则表达式时,这是一项令人不快的任务,因为我们错过了递归和其他有用的特性;我试着一次就做到了()但最后,我认为最好是匹配当时的(…);第一部分,然后解剖内部部分;e、 g.像这样:

const regex=/(然后\()(?:(?!\1 \\)*?\)/gs;
常量rxInner=/(?:function\s*\(?.*?)\s*\{\s+(?[\s\s]+?)\}、\s*function\s*\(?.*?)/gs;
const str=`promise()。然后(
函数(结果1){
...
},
函数(err){…}
);
..............................
那就答应我吧(
函数(结果2){
...
},
函数(err){…}
);`;
让match=regex.exec(str);
做{
控制台日志(匹配);
设m=rxInner.exec(匹配[0]);
做{
log(`a:${m.groups.a}
b:${m.groups.b}
c:${m.groups.c}`);
}while((m=rxInner.exec(匹配[0])!==null);
}while((match=regex.exec(str))!==null)

您的示例使用的是PCRE,还是在JavaScript中执行?我通过JavaScript代码运行这个正则表达式得到了相同的结果,在演示中,我用正则表达式101编写它。所以我认为如果它在regex101中工作,那么它在我的代码中也能工作。好吧,有一些像递归这样的功能可以在PCRE中工作,但不能在JavaScript中工作,这就是我问的原因。我编辑我的问题
console.clear();

const code = `
promise().then(
  function (result) {
    ...
  },
  function (err) { ... }
);

..............................

promise().then(
  function (result) {
    ...
  },
  function (err) { ... }
);
`;

const p = new RegExp(/then\([\n\s]+function\s*\((?<a>.*)\)\s*\{[\n\s]+(?<b>[\s\S]+)\},[\n\s]*function\s*\((?<c>.*)\)\s*\{[\n\s]*(?<d>[\s\S]+)?\}[\n\s]*\);/gmi);

code.replace(p, (x, a,b,c,d) => {

  console.log({ x, a, b, c });
});