views:

60

answers:

2

That is, given a Javascript file, this regular expression can show only the "immediate execution" of code, removing all the function definitions in the form of

function foo() {
  ...
}

Or can regular expression find the "matching { }" because there can many nested { }

+2  A: 

There is no such regular expression. The reason is based on theory of automata and grammars. The language

function foo() { ... }

can be generated by context free grammar, but not by regular grammar. For more info have a look at wikipedia.

Steves
A: 

Javascript is too complex a language to be reliably parsed using a regular expression. There's not just the problem you mentioned about the nested braces; there's also the fact that pretty much anything in a string should be ignored, that there's at least two ways to define a function, and a bunch of other issues.

Also, what would you do about function() { alert("Hello!"); }();? That's a function, but it executes immediately.

Basically, you'd need a javascript interpreter or debugger of some kind in order to see what runs and what doesn't.

cHao
Care to explain the downvote?
cHao