0

I'm reading some file contents and I want to match all function calls that is not test() in nodejs. test() can have any type of parameters and as many as possible. And all matched functions should include all the parameters including the closing ). E.g.

test({
  firstname: firstname({ userid: 12 }),
  lastname: lastname({ userid: 12 })
});

In the example above firstname and lastname is matched and the resulted matched string shoud be firstname({ userid: 12 })and lastname({ userid: 12 }).

einstein
  • 13,389
  • 27
  • 80
  • 110
  • If there was something stopping me I wouldn't asked – einstein Jul 14 '13 at 15:15
  • 1
    First, trying to parse code is a bad idea. Rethink your design. Second, you didn't specify the language of the code you're trying to parse. Javascript? Third, Regexes are not up to the task of matching parentheses. – John Dvorak Jul 14 '13 at 15:26
  • Well, now we have *sort of* a question. But it sure sounds like you have a set of requirements and you want someone to write the code for you. That's not what StackOverflow is about. If you've tried something, and you think it ought to work, but for some reason it doesn't, then you should ask about that. That's a specific problem based on code that you've actually written. But it's pretty unreasonable to take a complex task like this and just ask for a regex that'll do what you want. –  Jul 14 '13 at 15:27
  • But to answer your question... either write a JavaScript parser, or use an existing one and analyse its output. –  Jul 14 '13 at 15:29
  • Thanks for being so helpful! – einstein Jul 14 '13 at 15:49
  • Well, do you really expect someone to just write a solution to this enormously complex task for you? There's a wide variety of ways in which a function can be invoked, not to mention that you can have code in a program that looks like a function invocation, but isn't. You're expectations are entirely unreasonable. –  Jul 14 '13 at 16:18
  • Crazy Train I know now why you are crazy – einstein Jul 14 '13 at 16:28
  • 1
    "enormously complex task" - I solved it now with a one-liner – einstein Jul 14 '13 at 16:30
  • @Woho87: If you want all function calls, and you want to exclude things within `string` and `regex` literal syntax that merely look like function calls, then no you didn't. But at least you apparently did finally try something. –  Jul 14 '13 at 19:29

1 Answers1

0

Don't over-complicate yourself trying to parse that with regex alone. You may use them to aid you, but not for doing all the work.

For example, try using \b[a-zA-Z]+\({ (or even \b[a-zA-Z]+\s*\(\s*{ if your code isn't as clean as the example you put here) to match every function call, and then iterate the results to find those that don't call test.

Community
  • 1
  • 1
Racso
  • 2,310
  • 1
  • 18
  • 23
  • You really shouldn't pretend like this is anywhere close to an actual solution. There are *many* function calls that this will miss. Not to mention that you're not capturing the arguments. –  Jul 14 '13 at 16:24
  • I'm not "pretending" anything, but just trying to help. You should relax a bit, you know. Also, you could hint what kind of function calls would be missed by those expressions, in order to tweak the suggestion. – Racso Jul 14 '13 at 19:17
  • `(function() {})(args))` *(and other variations)* ... `foo["bar"](args)` ... `(foo)(args)` ... `foo["b" + a(args) + "r"](args)` ... `foo[(a + b() / c) % d](args)` ... `new Function("foo", "return foo + 'bar'")(args)` ... `foo(args)()()()()` ... and so on. –  Jul 14 '13 at 19:28
  • Based on the example given by the poster, I'm pretty sure that not even one of your variations is present on the file. Now I see you're not helping, but just flaming (or trying to show use your amazing javascript skills [?]). gl. – Racso Jul 15 '13 at 00:26
  • Go home and sleep Crazy Train. We don't need persons like you. – einstein Jul 15 '13 at 04:56