How to use regex to find and replace for new rxjs syntax?

198 views Asked by At

I want to upgrade my rxjs code to the new syntax. to do so I use vscode and "find and replace" by regex.

the old syntax:

.pipe(
 catchError((err) => {
   ...
 })
 )
 .subscribe((res) => {
   ...
 });

the new syntax is:

 .subscribe({ next: (res) => { ... }, error: (err) => { ... });

I try to do with this regex I built but I seems that the regex is wrong and not sure why because in that regex I find what between catchError and subscribe. So how to make it work in the correct way?

\.pipe(\s\ScatchError(.*).subscribe(.*)

Also how can I replace everywhere I match this pattern to new pattern? regex101.com

1

There are 1 answers

0
Wiktor Stribiżew On BEST ANSWER

You can use

^\.pipe\(\n\s*catchError([\w\W]*?\}\))\n\s*\)\s*\.subscribe([\w\W]*?\}\));$

See the regex demo.

Details:

  • ^ - start of a line
  • \.pipe\( - a literal .pipe( text
  • \n - a line break
  • \s* - 0+ whitespaces
  • catchError - a literal word
  • ([\w\W]*?\}\)) - Group 1: any zero or more chars, as few as possible, up to and including the }) substring
  • \n\s* - a line break and 0+ whitespaces
  • \) - a ) char
  • \s* - 0+ whitespaces
  • \.subscribe - a literal .subscribe string
  • ([\w\W]*?\}\)) - Group 2: any zero or more chars, as few as possible, up to and including the }) substring
  • ;$ - a ; char at the end of the line.