title: Extracting nested function names from a JavaScript function
body: <p>Given a function, I'm trying to find out the names of the nested functions in it (only one level deep).</p>
  
  <p>A simple regex against <code>toString()</code> worked until I started using functions with comments in them. It turns out that some browsers store parts of the raw source while others reconstruct the source from what's compiled; The output of <code>toString()</code> may contain the original code comments in some browsers. As an aside, here are my findings:</p>
  
  <h2>Test subject</h2>
  
  <pre><code>function/*post-keyword*/fn/*post-name*/()/*post-parens*/{
      /*inside*/
  }
  
  document.write(fn.toString());
  </code></pre>
  
  <h2>Results</h2>
  
  <pre>
  Browser      post-keyword  post-name  post-parens  inside
  -----------  ------------  ---------  -----------  --------
   Firefox      No            No         No           No
   Safari       No            No         No           No
   Chrome       No            No         Yes          Yes
   IE           Yes           Yes        Yes          Yes
   Opera        Yes           Yes        Yes          Yes
  </pre>
  
  <p>I'm looking for a cross-browser way of extracting the nested function names from a given function. The solution should be able to extract "fn1" and "fn2" out of the following function:</p>
  
  <pre><code>function someFn() {
      /**
       * Some comment
       */
       function fn1() {
           alert("/*This is not a comment, it's a string literal*/");
       }
  
       function // keyword
       fn2 // name
       (x, y) // arguments
       {
           /*
           body
           */
       }
  
       var f = function () { // anonymous, ignore
       };
  }
  </code></pre>
  
  <p>The solution doesn't have to be pure regex.</p>
  
  <p><strong>Update:</strong> You can assume that we're always dealing with valid, properly nested code with all string literals, comments and blocks terminated properly. This is because I'm parsing a function that has already been compiled as a valid function.</p>
  
  <p><strong>Update2:</strong> If you're wondering about the motivation behind this: I'm working on a new JavaScript unit testing framework that's called <a href="http://jsunity.com" rel="nofollow noreferrer">jsUnity</a>. There are several different formats in which you can write tests &amp; test suites. One of them is a function:</p>
  
  <pre><code>function myTests() {
      function setUp() {
      }
  
      function tearDown() {
      }
  
      function testSomething() {
      }
  
      function testSomethingElse() {
      }
  }
  </code></pre>
  
  <p>Since the functions are hidden inside a closure, there's no way for me invoke them from outside the function. I therefore convert the outer function to a string, extract the function names, append a "now run the given inner function" statement at the bottom and recompile it as a function with new <code>Function()</code>. If the test function have comments in them, it gets tricky to extract the function names and to avoid false positives. Hence I'm soliciting the help of the SO community...</p>
  
  <p><strong>Update3:</strong> I've come up with <a href="https://stackoverflow.com/questions/517411/extracting-nested-function-names-from-a-javascript-function/546984#546984">a new solution</a> that doesn't require a lot of semantic fiddling with code. I use the original source itself to probe for first-level functions.</p>