Я пытаюсь написать регулярное выражение для соответствия всем определениям метода 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'].
Вот что у меня пока есть, но у меня проблемы с теми, которые появляются в комментариях:
/(?<=this\.)\w*(?=\s*=\s*function\()/g
Я использовал этот крутой сайт , чтобы проверить его.
Как мне решить эту проблему?