0

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);
    }
4
  • 2
    What if those inner functions have inner functions themselves? Commented Jan 1, 2023 at 12:12
  • good question, need to think if so. but there is no such scenario as of now. Commented Jan 1, 2023 at 15:50
  • do you need to account for arrow functions too? Commented Jan 2, 2023 at 8:55
  • Where the correct answer posted yesterday is gone? Did the author or moderator deleted it? Commented Jan 3, 2023 at 2:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.