0

I'm looking for a reliable way to get function names from a string. The string values can be something like this:

let str = 'qwe(); asd();zxc()'
//or
let str = 'qwe("foo");asd(1);zxc();' 
//etc.

I want to have an array

['qwe', 'asd', 'zxc']

I tried str.split(';') but how do I get rid of parenthesis and anything they can hold? Is there a regexp that will match all symbols on the left of some other symbol?

7
  • Where is this string coming from? Commented Apr 14, 2017 at 15:28
  • 1
    What about strings like this? foo(bar(), baz()) Do they need to be covered too? Commented Apr 14, 2017 at 15:29
  • no, function's parameters don't count Commented Apr 14, 2017 at 15:32
  • Would it be possible for the parameters to have strings that could contain something that looks like a function? And are you going to allow for space between the function name and the opening (? Commented Apr 14, 2017 at 15:36
  • 1
    @Andrey It sounds like you're going to need to write a parser. Regular expressions cannot count nested parentheses. Commented Apr 14, 2017 at 15:41

2 Answers 2

1

You can use this simple regex to find function names in .match()

var str = "qwe(); asd();zxc()";
console.log(str.match(/\w+(?=\()/g));

Sign up to request clarification or add additional context in comments.

3 Comments

This will give the wrong result if the function calls contain any identifiers in their argument list.
Thanks, that's very close. What about this: var str = "qwe(); asd();zxc ()";?
@Andrey Your example hasn't such function. However use \w+\s*(?=\()
0

The first case it's fairly simple with regex a simple

[A-Za-z]\w+

would suffice.

on the second case it's a little bit trickier but maybe supressing the match for this

"(.*?)"

maybe a possibility

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.