Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:45:47+00:00 2026-06-16T00:45:47+00:00

Possible Duplicate: How to get function parameter names/values dynamically from javascript I’m currently working

  • 0

Possible Duplicate:
How to get function parameter names/values dynamically from javascript

I’m currently working on a project in javascript (node.js) that has me trying to get an array of parameter names (NOT values, I do not need arguments) from a function. I’m currently using Function.toString() to get the function string and then running a regex against that to get my parameter list.

Let’s take the following SIMPLE example:

var myFunction = function (paramOne, paramTwo) { ... }

Running my regex against this, and then doing some string magic (split, etc) I would expect an array back like this:

paramList = ['paramOne', 'paramTwo']

I have something that works but I’m feeling like it’s probably not the best solution given some of the funky characters javascript lets you use for variable names and that javascript will let you define functions on multiple lines.

Here is what I currently have:

function.*[\w\s$]*(\((.*[\w\s,$]*)\))

This gives me my “match” in group 1 and then my param list without parens in group 2, which is cool. Is this really the best way to do what I want? Is there a better regular expression I could use for this? I’m not really looking for something “simpler” but really just something that could catch all possible situations.

Any help would be appreciated, and many thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:45:49+00:00Added an answer on June 16, 2026 at 12:45 am

    Preface: By far, the best way to handle this is to use a JavaScript parser rather than trying to do it with a single regular expression. Regular expressions can be part of a parser, but no one regular expression can do the work of a parser. JavaScript’s syntax (like that of most programming languages) is far too complex and context-sensitive to be handled with a simple regular expression or two. There are several open source JavaScript parsers written in JavaScript. I strongly recommend using one of those, not what’s below.


    The easiest thing would be to capture everything in the first set of parens, and then use split(/\s*,\s*/) to get the array.

    E.g.:

    var str = "function(   one  ,\ntwo,three   ,   four   ) { laksjdfl akjsdflkasjdfl }";
    var args = /\(\s*([^)]+?)\s*\)/.exec(str);
    if (args[1]) {
      args = args[1].split(/\s*,\s*/);
    }
    console.log("args: ", args);

    How the above works:

    1. We use /\( *([^)]+?) *\)/ to match the first opening parenthesis (\( since ( is special in regexes), followed by any amount of optional whitespace, followed by a capture group capturing everything but a closing parenthesis (but non-greedy), followed by any amount of optional whitespace, followed by the closing ).

    2. If we succeed, we split using /\s*,\s*/, which means we split on sequences which are zero or more whitespace characters (\s*) followed by a comma followed by zero or more whitespace characters (this whitespace thing is why the args in my example function are so weird).

    As you can see from the example, this handles leading whitespace (after the ( and before the first argument), whitespace around the commas, and trailing whitespace — including line breaks. It does not try to handle comments within the argument list, which would markedly complicate things.

    Note: The above doesn’t handle ES2015’s default parameter values, which can be any arbitrary expression, including an expression containing a ) — which breaks the regex above by stopping its search early:

    var str = "function(   one  ,\ntwo = getDefaultForTwo(),three   ,   four   ) { laksjdfl akjsdflkasjdfl }";
    var args = /\(\s*([^)]+?)\s*\)/.exec(str);
    if (args[1]) {
      args = args[1].split(/\s*,\s*/);
    }
    console.log("args: ", args);

    Which brings us full circle to: Use a JavaScript parser. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Get query string values in JavaScript I am writing a jQuery function
Possible Duplicate: Passing multiple parameter to PHP from Javascript I am currently trying to
Possible Duplicate: How can I get the name of function inside a JavaScript function?
Possible Duplicate: How can I get a specific parameter from location.search? I have a
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Returning multiple values from a C++ function /************************************************/ /* Name: premserv */
Possible Duplicate: Get property of object in JavaScript var Terminal = function() { this.walk
Possible Duplicate: Get query string values in JavaScript how can i get the get
Possible Duplicate: javascript replace function not working Here is my JS code: var imgTitle
Possible Duplicate: Get query string values in JavaScript Say if,i've a URL like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.