views:

160

answers:

3

Hi,

I am trying to write a regex to match all the javascript method definitions in a constructor string.

//These two should match
this.myMethod_1 = function(test){ return "foo" }; //Standard
this.myMethod_2 = function(test, test2){ return "foo" }; //Spaces before

//All of therse should not
//this.myMethod_3 = function(test){ return "foo" }; //Comment shouldn't match
/**
 *this.myMethod_4 = function(test){ return "foo" }; //Block comment shouldn't match
 */

//       this.myMethod_5 = function(test){ return "foo" }; //Comment them spaces shouldn't match

/*
 *        this.myMethod_6 = function(test){ return "foo" }; //Block comment + spaces shouldn't match
 */

this.closure = (function(){ alert("test") })(); //closures shouldn't match

The regex should match ['myMethod_1', 'myMethod_2'] The regex should not match ['myMethod_3', 'myMethod_5', 'myMethod_6', 'closure']

Here's what I have so far, but I am having problems with the ones that appear in comments:

/(?<=this.)\w*(?=\s*=\s*function()/g

I've been using this cool site to test it.

Does anyone have any ideas how to solve this.

Regards,

Chris

+5  A: 

this sounds complicated to do it correctly. you will need to create a parser for this, a simple regexp will most likely not make it.

a very good starting point is Narcissus, which is a javascript parser written in .. javascript.

it is just 1000 lines of code, it should be possible to extract just the method-matching parts of it.

Andreas Petersson
A: 

add a ^\s* to the begining might help, it's not perfect, but it will work for your test cases

cobbal
A: 

One regex might be difficult to write and debug. Have you thought of writing several regexes, one for each line that should either match to confirm or reject a piece of code?

e.g.

/(?<=this.)\w*(?=\s*=\s*function()/g  // matches a simple constructor
/^\/\// // if it matches then this line starts with a comment

and so on

1800 INFORMATION