views:

35

answers:

1

I'm wondering the best way to approach this. I have a JavaScript file in my web app that has a bunch of different methods all with the same signature.

What I want to do is read in the JS file server side and parse out all the method names of only the methods that have that certain signature.

Let's say my JS file looked like below:

function DoSomething(s1, s2) { }

function DoSomething2(s1, s2) { }

function DoSomething3(s1, s2, s3) { }

Let's say the signature I was looking for was (s1, s2). I would want to process the JavaScript file and the result would be a List that had DoSomething and DoSomething2 in it.

Probably the best way to tackle this would be with Regex and String parsing at this point?

+1  A: 
/function\s+(\w+)\s*\(\s*s1\s*,\s*s2\s*\)/

Should do it, but it might be wise to also check for functions being assigned to variables:

/(\w+)\s*(?:=|:)\s*function\s*\(\s*s1\s*,\s*s2\s*\)/

That would match code like this:

var dudewhat = function(s1, s2) {}

zomg: function(s1,s2) {}

Of course it will not match functions being assigned to arrays/objects with bracket notation, like object[property] = function() {}, but given you want a function name those kinds of assignments might not be important.

MooGoo
the complexity comes in when trying to find the function body, since there can be many open and close curly braces.
dave thieben
Yes of course, but the question only asked for the "method names", not the function body.
MooGoo