我需要有两个输出值:
1. 我正在查找的命名函数的整个主体。 2. 找到的命名函数已从完整输入脚本中移除。
因此,假设我在此输入脚本中寻找
findMe
函数:function() {
function something(x,y) {
if (x == true) {
console.log ("Something says X is true");
// The regex should not find this:
console.log ("function findMe(z) { var a; }");
}
}
function findMe(z) {
if (z == true) {
console.log ("Something says Z is true");
}
}
findMe(true);
something(false,"hello");
}();
从这里,我需要以下两个结果值:
The extracted
findMe
scriptfunction findMe(z) { if (z == true) { console.log ("Something says Z is true"); } }
The input script with the
findMe
function removedfunction() { function something(x,y) { if (x == true) { console.log ("Something says X is true"); // The regex should not find this: console.log ("function findMe(z) { var a; }"); } } findMe(true); something(false,"hello"); }();
要查找的脚本正文可能包含任何有效的JavaScript代码。查找此脚本的代码或正则表达式必须能够忽略字符串中的值,多个嵌套的块级等。
如果要查找的函数定义在字符串内部指定,则应将其忽略。
有关如何完成此类操作的任何建议?
更新:
看起来正则表达式不是解决这个问题的正确方式。我愿意听取可以帮助我完成此任务的解析器的指针。我正在查看Jison,但很想听听其他内容。
findMe()
函数,脚本将会出错,因为仍然有调用它的代码。此外,考虑到您要查找即使嵌入在其他函数中的函数,您确定不会有多个具有您要查找的名称的函数吗? - nnnnnn