1

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

1
  • try obj\.methodname\(([\w\s,]*)\) Commented Jan 28, 2016 at 5:50

1 Answer 1

3

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"]
Sign up to request clarification or add additional context in comments.

3 Comments

what if i pass a function result: obj.method( "la (LA)".bold(), 342.43); ?
@dandavis that is fine, but this solution will fail if the function call in the argument takes another set of arguments, like obj.method( "la (LA)".bold(10, 20), 342.43). This case is not regular language so we may need a more powerful language to take care of that, and not preferred to be solved using Regex.
thats ok, it served my purpose, as I know the function call and its possible arguments, and looking for a specific function call

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.