0

我有一个 js 文件的文本。我想找到函数的所有名称(例如“destroy”、“create”),但我只想找到“第一”级函数——“close: function”或“success: function”不应该匹配。我的正则表达式:\,+\s+(\S+:)\s+function 现在我找到了所有函数名称。请帮我处理我的正则表达式。

(function () {
    Create("object", {
            customProperty: {
                a: 1,
                b: 2
            },
            property1: 1,
            property2: 2,
            init: function () {

            },
            listeners: {
                close: function (a, b) {
                    return 1;
                }
            }
            destroy: function () {

            },
            create: function () {
                request({
                    success: function (response, record) {}
                });
            }

        }
    }

).call(this);
4

1 回答 1

0

This is provably impossible with standard regular expressions. In fact, this is one of the "classic" impossible problems, where CS students are asked to prove that this problem is impossible to solve with regular expressions.

It may be possible with some variation of extended regular expressions: JavaScript allows backreferences but I don't think this is enough.

You must find an alternate solution.

See: Can regular expressions be used to match nested patterns?

于 2013-10-17T23:01:08.850 回答