Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>&lt;pre&gt; &lt;script type="text/javascript"&gt; 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 */ } function fn3() { alert("this is the word function in a string literal"); } var f = function () { // anonymous, ignore }; } var s = someFn.toString(); // remove inline comments s = s.replace(/\/\/.*/g, ""); // compact all whitespace to a single space s = s.replace(/\s{2,}/g, " "); // remove all block comments, including those in string literals s = s.replace(/\/\*.*?\*\//g, ""); document.writeln(s); // remove string literals to avoid false matches with the keyword 'function' s = s.replace(/'.*?'/g, ""); s = s.replace(/".*?"/g, ""); document.writeln(s); // find all the function definitions var matches = s.match(/function(.*?)\(/g); for (var ii = 1; ii &lt; matches.length; ++ii) { // extract the function name var funcName = matches[ii].replace(/function(.+)\(/, "$1"); // remove any remaining leading or trailing whitespace funcName = funcName.replace(/\s+$|^\s+/g, ""); if (funcName === '') { // anonymous function, discard continue; } // output the results document.writeln('[' + funcName + ']'); } &lt;/script&gt; &lt;/pre&gt; </code></pre> <p>I'm sure I missed something, but from your requirements in the original question, I think I've met the goal, including getting rid of the possibility of finding the <code>function</code> keyword in string literals.</p> <p>One last point, I don't see any problem with mangling the string literals in the function blocks. Your requirement was to find the function names, so I didn't bother trying to preserve the function content.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload