Regular Expressions 101

Javascript function declaration to arrow function declaration regex

Open Workspace
  • PCRE2 (PHP)
  • Match
0

Regular Expression

/
function (?<name>[a-zA-Z0-9]+)\s*(?<arguments>\([^()]*\)(?::\s*[^{}\s]*)?)\s*(?<body>\{[^\r{}]*(?:\{[^\r{}]*(?:\{[^\r{}]*(?:\{[^\r{}]*(?:\{[^\r{}]*(?:\{[^\r{}]*\}[^\r{}]*)*\}[^\r{}]*)*\}[^\r{}]*)*\}[^\r{}]*)*\}[^\r{}]*)*\})(?:;)?
/
gm

Description

This regex takes a function declaration and transforms it to an arrow function.

Replace: const $1 = $2 => $3;

Submitted by Anonymous