I’m working on a large and extremely messy javascript file, and I would like to remove all functions from the file, ultimately creating a version which contains only data.
the code looks something like this:
var foo : bar = "hi";
function foobar (){
//blah blah
}
var fobar:bar;
var barfo:bar;
function imSoUgly(){
//Blah blah blah blah mr freeman
}
The regex I would like to build would find all function.{.} and delete them, producing this:
var foo : bar = "hi";
var fobar:bar;
var barfo:bar;
I’m not quite sure where to start with this. Ideally I would like to do it with Textmate’s RegEx, but I’m easy.
In my opinion, Regex is not sufficient to do something as complex as this is. The best I could do with regex is this:
That will remove all the functions in your example, but if you had something like this, it wouldn’t work:
You would still have:
Pessimist’s answer would work, but ONLY if all of the functions have no spaces before the closing line, which is unlikely to be true.
The bottom line is that you really need a real JavaScript parser. A quick google search found this:
http://www.antlr.org/