Regular Expressions 101

Community Patterns

Javascript function declaration to arrow function declaration regex

0

Regular Expression
PCRE2 (PHP >=7.3)

/
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 - 3 years ago