Regular Expressions 101

Community Patterns

Community Library Entry

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 - 4 years ago