PHP regex to fetch the inner function list from JavaScript source code

54 Views Asked by At

Below is the JavaScript source code on which the php regex I am trying to apply.

<script>
function outerfunction () {

    function abcd () {
        return ('efgh', 1);
    }

    function efgh () {
        return ('higk', 2);
    }
}
</script>

I am trying to fetch all the inner function from the JavaScript code below with function name length having 4 chars, using preg_match_all() with pattern /(function.[a-z]{4}.[(][)].*)}/s. using this online tool phpliveregex.com

below is the output in 2nd array. but i need all the functions list as in individual array elements. currently it is comming inside single array element result. php regex output

Current output

array[1] => function abcd () {
        return ('efgh', 1);
    }

    function efgh () {
        return ('higk', 2);
    }

Needed:

array[1] => function abcd () {
        return ('efgh', 1);
    }

array[2] => function efgh () {
        return ('higk', 2);
    }
0

There are 0 best solutions below