【发布时间】:2009-03-19 07:43:43
【问题描述】:
我正在尝试编写一个正则表达式来匹配构造函数字符串中的所有 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
正则表达式应匹配 ['myMethod_1', 'myMethod_2']。正则表达式不应匹配 ['myMethod_3', 'myMethod_5', 'myMethod_6', 'closure']。
这是我目前所拥有的,但我遇到了出现在 cmets 中的问题:
/(?<=this\.)\w*(?=\s*=\s*function\()/g
我一直在用这个很酷的site 来测试它。
我该如何解决这个问题?
【问题讨论】:
标签: javascript regex parsing