How to get function body text in JavaScript?
Since you want the text between the first {
and last }
:
derp.toString().replace(/^[^{]*{\s*/,'').replace(/\s*}[^}]*$/,'');
Note that I broke the replacement down into to regexes instead of one regex covering the whole thing (.replace(/^[^{]*{\s*([\d\D]*)\s*}[^}]*$/,'$1')
) because it's much less memory-intensive.
NOTE: The accepted answer depends on the interpreter not doing crazy things like throwing back comments between 'function' and '{'. IE8 will happily do this:
>>var x = function /* this is a counter-example { */ () {return "of the genre"};
>>x.toString();
"function /* this is a counter-example { */ () {return "of the genre"}"
var entire = derp.toString();
var body = entire.slice(entire.indexOf("{") + 1, entire.lastIndexOf("}"));
console.log(body); // "a(); b(); c();"
Please use the search, this is duplicate of this question