0

I have a text of js File. I want to find all name of function(e.g "destroy","create") but i want to find only "first" level of functions - "close: function " or "success: function" should not be matched. My regex: \,+\s+(\S+:)\s+function For now i found all function names. Please help me with my regex.

(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);
Ilya.R
  • 41
  • 7
  • I don't think you understood the intent behind regular expressions. Can you do this if you construct a *long* string and ask for *first* level functions out of that long string? (Of course, while retaining whatever whitespace you currently have) – S.R.I Oct 17 '13 at 23:02
  • Yes , I constructed a long string, but i can't to understand how regex should be – Ilya.R Oct 17 '13 at 23:17
  • @LALALA: That's because it's impossible. – Dietrich Epp Oct 17 '13 at 23:19
  • 2
    @LALALA, as Dietrich said, it's not possible to do this with regex. You have to find other alternatives. Regex is not the cure for [everything](http://3d.xkcd.com/208/)! – S.R.I Oct 17 '13 at 23:22

1 Answers1

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?

Community
  • 1
  • 1
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415