Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
function\s+(?<functionName>\w+)\s*\((?<functionArguments>(?:[^()]+)*)?\s*\)\s*(?<functionBody>{(?:[^{}]+|(?-1))*+})
/
g

Description

This will match all named functions.

PHP code to print: $js = file_get_contents('http://somedomain.com/javascript.js'); $regex = "/function\s+(?<functionName>\w+)\s*((?<functionArguments>(?:[^()]+))?\s)\s*(?<functionBody>{(?:[^{}]+|(?-1))*+})/"; preg_match_all($regex, $js, $jsFunctions, PREG_PATTERN_ORDER); echo "<pre>"; echo "<BR><BR>Functions<BR><BR>"; print_r($jsFunctions[1]); //name, arguments is $jsFunctions[2], body is $jsFunctions[3] echo "</pre>";

Submitted by Alan Halls - 8 years ago