正则表达式,用于查找一段代码中的所有方法

Chr*_*mbo 1 javascript regex parsing

我正在尝试编写一个正则表达式来匹配构造函数字符串中的所有JavaScript方法定义.

//These two should match
this.myMethod_1 = function(test){ return "foo" }; //Standard
this.myMethod_2 = function(test, test2){ return "foo" }; //Spaces before

//All of these should not
//this.myMethod_3 = function(test){ return "foo" }; //Comment shouldn't match
/**
 *this.myMethod_4 = function(test){ return "foo" }; //Block comment shouldn't match
 */

//       this.myMethod_5 = function(test){ return "foo" }; //Comment them spaces shouldn't match

/*
 *        this.myMethod_6 = function(test){ return "foo" }; //Block comment + spaces shouldn't match
 */

this.closure = (function(){ alert("test") })(); //closures shouldn't match
Run Code Online (Sandbox Code Playgroud)

正则表达式应匹配['myMethod_1','myMethod_2'].正则表达式不应与['myMethod_3','myMethod_5','myMethod_6','closure']相匹配.

这是我到目前为止所做的,但我对评论中出现的问题有疑问:

/(?<=this\.)\w*(?=\s*=\s*function\()/g
Run Code Online (Sandbox Code Playgroud)

我一直在使用这个很酷的网站来测试它.

我该如何解决这个问题?

And*_*son 5

这听起来很复杂.您需要为此创建一个解析器,一个简单的正则表达式很可能不会成功.

一个非常好的起点是Narcissus,它是一个用JavaScript编写的JavaScript解析器.

它只有1000行代码.应该可以只提取它的方法匹配部分.