javascript get function body

后端 未结 8 1701
一整个雨季
一整个雨季 2020-12-01 05:44

I have a function e.g.

var test = function () {alert(1);}

How can I get the body of this function?

I assume that the only way is to

8条回答
  •  广开言路
    2020-12-01 06:09

    Try this:

    /\{(\s*?.*?)*?\}/g.exec(test.toString())[0]
    

    test.toString() will hold your entire declaration.

    /{(\s*?.?)?}/g will match everything between your braces

提交回复
热议问题