-1

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.

mjames
  • 220
  • 2
  • 13
  • Very crude try: `function\s*.+\(.*\)\s*{.+}`, multiline, Ruby-flavored, case-insensitive. – Evadne Wu Jun 28 '10 at 01:15
  • Although I don't think regex will work for this (see my comment below), this is a great site for figuring out regex: http://gskinner.com/RegExr/ – Computerish Jun 28 '10 at 01:21
  • Consider: `var s = "function foo() {"; var x = 42; s = "}";` – Bart Kiers Jun 28 '10 at 06:51
  • @Bart K. ;-) But, I challenge you to provide a real-life example of such a string. Bonus if you can direct the lynch mob. – Brock Adams Jun 29 '10 at 12:34
  • The easiest solution is just using a regex, but checking every occurrence of it rather than using a blind "replace all". Should be good enough and spare lots of time. – Florian Pilz Jan 06 '11 at 14:31

5 Answers5

3

I don't think it is possible to do this with only with regular expressions, as it is not possible to match starting and ending braces (code blocks) which can be arbitrary deeply nested.

To do this reliably, you would need to recursively look through all the inner code code blocks to locate the end of the function. Or something like that (count the number of braces, ...).

Darko Kenda
  • 4,781
  • 1
  • 28
  • 31
1

You can't. That being said you could use something like this

function\s+\w+\s*\([^)]*\)\s*{[^}]*}

but it will fail if there are any { or } inside the function and you can't do anything about this

Diadistis
  • 12,086
  • 1
  • 33
  • 55
0

-- Deleted - Carko is right, a regexp would be a very naive approach to the problem.
You need a PEG for that.

PeterM
  • 1,188
  • 1
  • 10
  • 15
0

In my opinion, Regex is not sufficient to do something as complex as this is. The best I could do with regex is this:

[\r\n]function [\w ]*\(\)\{[\w\W]*?}

That will remove all the functions in your example, but if you had something like this, it wouldn't work:

function foobar (){
   if(condition){
      // do something
   } // this end brace would be mis-interpreted as the end of the function
   // bla, bla, bla
}

You would still have:

   // bla, bla, bla
}

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/

Computerish
  • 9,590
  • 7
  • 38
  • 49
0

You can't do this with a "regular" expression, but some languages provide pattern-matching constructs which allow you to match (among other things) balanced text.

For example, Perl:

/function\s*\(\)\s*(\{([^{}]++|(?1))*\}/

Whether it's the correct tool for the job (HINT: It probably isn't) is another question entirely.

Anon.
  • 58,739
  • 8
  • 81
  • 86