如何获得一个函数的身体作为字符串? [英] How to get a functions's body as string?
本文介绍了如何获得一个函数的身体作为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
函数A(){
我想知道如何将函数的主体转换为字符串?警报(1);
}
输出= eval(A).toString()//这将与函数A(){〜}
//输出输出 - >函数A(){alert(1); }
//如何使输出变为alert(1);只要???
解决方案
如果您要做一些难看的事情,它与正则表达式:
A.toString()。match(/ function [^ {] + \ {([\ s\S] *)\} $ /)[1];
I want to know how to convert a function's body into a string?
function A(){
alert(1);
}
output = eval(A).toString() // this will come with function A(){ ~ }
//output of output -> function A(){ alert(1); }
//How can I make output into alert(1); only???
解决方案
If you're going to do something ugly, do it with regex:
A.toString().match(/function[^{]+\{([\s\S]*)\}$/)[1];
这篇关于如何获得一个函数的身体作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文