2

I have an input like this

var example = ...

var foobar = function() {
    var barfoo = function() {

    }
}


var something = ...

I want to extract the function foobar.

How can I extract the text from var foobar until a closing bracket which is at the first indentation level?

preg_match('#var foobar =.*^}/#ms', $this->body, $matches);

Does not give any matches.

Alex
  • 27,292
  • 13
  • 89
  • 143

1 Answers1

-1

This returns what you want:

preg_match('#var foobar =.*}#ms', $this->body, $matches);

But it will not work if you fill the dots at the end with more functions.

Regular expressions are the wrong tool for the job because you are dealing with nested structures, i.e. recursion.

Regular Expression to match outer brackets

Community
  • 1
  • 1
PiTheNumber
  • 20,216
  • 13
  • 96
  • 165
  • I thought so. That's why I added the quote and the link. You need a JavaScript Parser. Maybe you can use this: https://github.com/kbjr/UglifyJS.php/blob/master/javascript-parser.php – PiTheNumber Jul 24 '15 at 13:40