Javascript 如何使用正则表达式将承诺模式转换为可观察模式?

Javascript 如何使用正则表达式将承诺模式转换为可观察模式?,javascript,regex,visual-studio-code,Javascript,Regex,Visual Studio Code,我有一个非常长的代码(在许多文件中)具有promise模式,我想将该模式转换为可观察的模式 我在vscode中通过查找和替换来完成它 因此,我创建了一个正则表达式来匹配许多变体(如\n和空格)。 所有承诺代码都是相同的模式 从承诺模式: this.service.foo().then( function(res) { .... }, function(err) { ... } ); this.servi

我有一个非常长的代码(在许多文件中)具有promise模式,我想将该模式转换为可观察的模式

我在vscode中通过查找和替换来完成它

因此,我创建了一个正则表达式来匹配许多变体(如\n和空格)。 所有承诺代码都是相同的模式

从承诺模式:

   this.service.foo().then(
     function(res) {
         ....
     },
     function(err) {
       ...
     }
    );
   this.service.foo()
    .pipe(catchError(err => { 
      ...
      return err; 
    }))
    .subscribe(res => { ... });
(?s)then\(\s+function\(res\)\s*\{\s+(.+)\},\s*function\(err\)\s*\{\s*(.+)\}\s*\);
\npipe(catchError(err => {\n$2\nreturn err;\n}))\n.subscripe(res => {\n$1\n\});
要查看可观察的模式:

   this.service.foo().then(
     function(res) {
         ....
     },
     function(err) {
       ...
     }
    );
   this.service.foo()
    .pipe(catchError(err => { 
      ...
      return err; 
    }))
    .subscribe(res => { ... });
(?s)then\(\s+function\(res\)\s*\{\s+(.+)\},\s*function\(err\)\s*\{\s*(.+)\}\s*\);
\npipe(catchError(err => {\n$2\nreturn err;\n}))\n.subscripe(res => {\n$1\n\});
如何使用正则表达式将承诺模式转换为可观察模式?我对正则表达式的问题是我不知道如何匹配两个“占位符”并将其转换为新语法

任何帮助都将不胜感激

使用模式:

   this.service.foo().then(
     function(res) {
         ....
     },
     function(err) {
       ...
     }
    );
   this.service.foo()
    .pipe(catchError(err => { 
      ...
      return err; 
    }))
    .subscribe(res => { ... });
(?s)then\(\s+function\(res\)\s*\{\s+(.+)\},\s*function\(err\)\s*\{\s*(.+)\}\s*\);
\npipe(catchError(err => {\n$2\nreturn err;\n}))\n.subscripe(res => {\n$1\n\});
在这个模式中,我使用了:

  • (?s)
    标志,该标志允许
    匹配换行符
  • 假设lambdas中的参数将命名为
    res
    err
  • 捕获组
    (…)
    以捕获lambda的主体
最后,我使用了替换模式:

\npipe\(catchError\(err => \{\n\2\nreturn err;\n\}\)\)\n\.subscripe\(res => \{\n\1\n\}\);

使用Notepad++完成的示例:

this.service.foo().then(
     function(res) {
         var s = res;
         console.log("result of a promise is " + s);
     },
     function(err) {
         cnosole.error("Something went wrong, error: " + err);
     }
    );
输出:

this.service.foo().
pipe(catchError(err => {
cnosole.error("Something went wrong, error: " + err);
     
return err;
}))
.subscripe(res => {
var s = res;
         console.log("result of a promise is " + s);
     
});
不过,您需要注意格式设置

编辑与代码调整

VS代码不识别标志,因此我们需要在开始时删除(?s),这允许匹配新行,使用该更改,我们需要将更改为[\s\s],该模式匹配“空白或非空白”,因此所有内容:)

另外,VS Code与\s不匹配\n,这就是为什么我将\s替换为[\s\n]

另外,捕获组使用
$
登录替换模式进行引用

更正后:

模式:

then\([\n\s]+function\(res\)\s*\{[\n\s]+([\s\S]+)\},[\n\s]*function\(err\)\s*\{[\n\s]*([\s\S]+)\}[\n\s]*\);
替换模式:

   this.service.foo().then(
     function(res) {
         ....
     },
     function(err) {
       ...
     }
    );
   this.service.foo()
    .pipe(catchError(err => { 
      ...
      return err; 
    }))
    .subscribe(res => { ... });
(?s)then\(\s+function\(res\)\s*\{\s+(.+)\},\s*function\(err\)\s*\{\s*(.+)\}\s*\);
\npipe(catchError(err => {\n$2\nreturn err;\n}))\n.subscripe(res => {\n$1\n\});

在VSC中,还不能在正则表达式中指定标志。要模拟
s
标志,必须使用
[\s\s]
而不是
。这些组在替换字符串中用
$n
引用

搜索:

然后\(\n\s+函数\(res\)\s*\{\n([\s\s]+?)\}、\s*函数\(err\)\s*{\n([\s\s]+)\}\s*\);
替换:

\npipe\(catchError\(err=>\{\n$2\n返回错误;\n\}\)\n\.subscribe\(res=>\{\n$1\n\}\);
范例


问题是您不能在正则表达式中计算
{
}
,因此如果在
err
函数中有任何块语句(如
if
),正则表达式可能匹配,但替换结果将是错误的。

在正则表达式101中,您设置的风格是PHP。我想这就是为什么它不能在vscode中工作,我需要它在js中。在js中可以做些什么吗?