Uttara
Uttara

Reputation: 2534

Regex to match a specific function call and capture arguments using javascript

I want to look for a specific function call statement and if found need to extract the arguments using javascript regex.
for ex: say obj.methodname(arg1,arg2) or obj.methodname(arg1, arg2)
notice the space between the arguments which is possible and the number of arguments is also not fixed.

I tried various regex for the same from SO itself but couldn't get it to work
one of them is /obj.methodname([\w,]*)/.exec(text) but not getting the arguments, only getting methodname

please help

Upvotes: 1

Views: 1969

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

Something like,

> "obj.methodname(arg1,arg2)".match(/obj.methodname\((.*)\)/)[1].split(",")
< ["arg1", "arg2"]

Further, you can trim the values to get rid of the spaces

> ["arg1", " arg2"].map(function(value){ return value.trim() })
< ["arg1", "arg2"]

Upvotes: 3

Related Questions