I need to find all function signatures accepting more than X arguments (say 2).
I tried something like function\s*\((\w*,){3,10} (which would catch all signature with 3-10 args, but it did not work. Variations on it are yielding unexpected results. I guess I'm just not that good at regex, but any help is appreciated.
update: I should point out that I am writing a sort of code inspection tool. Among the many things, I want to spot functions that accept more than 2 arguments (as I promote the usage of functions with few arguments, and 1 argument in case of constructors). So I cannot call arguments.length etc.
Just think "easy":
(...): \(\), inside the brackets: \(,{2,2}\) , NEEDS to be preceeded AND followed by strings: \((?:\w+,\w+){2,2}\)\((?:\w+,\w*){2,2}\w+\)function name: function\s+\w+\s*\((?:\w*,\w*){2,2}\)function\s+\w+\s*\((?:\s*\w+\s*,\s*\w*\s*){2,2}\w+\s*\)There you go. This should cover all "common" method declarations, except nameless lambda-expressions:
function\s+\w+\s*\((?:\s*\w+\s*,\s*\w*\s*){2,2}\w+\s*\)

Debuggex Demo
Matching two to two commas will find signatures with 3 parameters. Matchint two to five commas will find signatures with 3 upto 6 parameters.
First of all, JavaScript is not a regular language, as a result, one cannot use a regex to fully grasp the language, and thus there is a possibility that you will either accept false positives, or false negatives.
A regex that probably comes close is:
function(?:\s+\w+)*\s*\(([^),]*)(\s*,\s*[^),]*){2,}\)
The regex works as follows:
function searches for the function keyword.\s+\w+ this group is used to identify with the name of the function: it is possible to define an anonymous function with no name, so the group must be optional.\s*\( there is an arbitrary number of space and a bracket to open the parameter list;[^,)]* (a sequence of characters not containing a comma nor the closed bracket).\s*,\s* pattern (\s* is actually unnecessary). Next again a group for a parameter name and of course we need to skip at least two commas.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With