I am struggling with building a regular expression to match js function parameters. I working on a project where I need to parse javascript and I need regex that will match everything between parentheses for functions.
function foo (bar) {} // should match 'bar'
Foo.bar = function (baz, obj) {} // should match only 'baz, obj'
It should only match stuff with after 'function' or 'function foo', basically so it doesn't match things between paren inside of function bodies. I need it to exclude the actual '(' and ')' chars. Also I the whitespace between 'function' or the function name and the opening '(' can be infinite.
Here is what I have so far and I am only getting matches for the second example and only if there is just one space between the 'function' and '('. Regex isn't my strong suit so this may not even be possible but Thanks in advance!
(?<=,|\()([#a-z0-9]+)(?=(,?[#a-z0-9]+)+\)|\))
Update: This is another expression I have trying to get working. It is closer than the one above but only allows exactly one space between 'function' and '('. also it doesn't cover case 1 above.
\b(?<=(function.)\()([^),]+)[^)]*\b
function?