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

  • Home
  • SEARCH
  • 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 8560801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:21:36+00:00 2026-06-11T16:21:36+00:00

How do I find out the name of a javascript function parsed in Java.

  • 0

How do I find out the name of a javascript function parsed in Java.
I allow a user to type in a Javascript function (in a JTextArea), then I use ScriptEngineManager to confirm it is valid syntax as follows:

public final boolean isFunctionValid(String function)
{
    try
    {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        Object result = engine.eval(function);
    }
    catch(ScriptException se)
    {
        return false;
    }
    return true;
}

Works fine, but I also want to know the name of the function so I can assign the function a name that matches the function name. Id prefer it if ScriptEngineManager does it for me rather tham me trying to work it out by parsing the variable function with al the inherent risks of that approach

EDIT
As it seems there is no way to properly do this, Ive created two checks using regular expressions that work for me.

    protected static Pattern functionNameMatch;
    protected static Pattern multipleFunctionsMatch;
    static
    {
        functionNameMatch = Pattern.compile("function[ ]+([a-zA-Z0-9]*)[ ]*\\(");
        multipleFunctionsMatch = Pattern.compile("function[ ]+");
    }


    protected boolean isOneFunction()
    {
        Matcher m = multipleFunctionsMatch.matcher(function.getText());
        if(m.find())
        {
            if(m.find())
            {
                return false;
            }
            return true;
        }
        return false;
    }

    protected String deriveNameFromFunction()
    {
        Matcher m = functionNameMatch.matcher(function.getText());
        if(m.find())
        {
            return m.group(1);
        }
        return "";
    }
  • 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-11T16:21:37+00:00Added an answer on June 11, 2026 at 4:21 pm

    So as it was not possible in JavaScript, I do some additional checks using pattern matching instead

    protected static Pattern functionNameMatch;
    protected static Pattern multipleFunctionsMatch;
    static
    {
        //TODO this method only works if all parameters are put on first line of function, otherwise declares function
        //is invalid
        functionNameMatch = Pattern.compile("function[ ]+([a-zA-Z0-9]*[ ]*\\(.*\\))");
        multipleFunctionsMatch = Pattern.compile("function[ ]+");
    }
    
    protected boolean isOneFunction()
    {
        Matcher m = multipleFunctionsMatch.matcher(function.getText());
        if(m.find())
        {
            if(m.find())
            {
                return false;
            }
            return true;
        }
        return false;
    }
    
    protected String deriveNameFromFunction()
    {
        Matcher m = functionNameMatch.matcher(function.getText());
        if(m.find())
        {
            return m.group(1);
        }
        return "";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I find out the name of the fields in a PDF file
i just want to find out the name of this grid to refresh it.
Okay, so what I'm attempting to do is find out the name of the
I'm trying to find out how to programmatically (I'm using C#) determine the name
Is there any way to (programatically) find out what your app's name is? I'm
I have a javascript array object like {arr = {0:{name:henry,role:user},1:{name:mark,role:admin}} I have a html
So i have this code: <html> <head> <title>Form</title> <script type=text/javascript> function showConfirmationDialog() { var
Given a function, I'm trying to find out the names of the nested functions
I'm trying to use JavaScript to find the context menu items being shown and
I am creating a JavaScript function that will take a user inputted value and

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.