artemean
artemean

Reputation: 855

How to parse functions names from string using javascript?

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?

Upvotes: 0

Views: 81

Answers (2)

Manuel Morales
Manuel Morales

Reputation: 106

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

Upvotes: 0

Mohammad
Mohammad

Reputation: 21489

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

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

Upvotes: 1

Related Questions