简体   繁体   English

正则表达式匹配特定的函数调用和使用javascript捕获参​​数

[英]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. 我想查找一个特定的函数调用语句,如果发现需要使用javascript正则表达式提取参数。
for ex: say obj.methodname(arg1,arg2) or obj.methodname(arg1, arg2) 例如:说obj.methodname(arg1,arg2)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 我从SO本身尝试了各种正则表达式,但无法使它工作
one of them is /obj.methodname([\\w,]*)/.exec(text) but not getting the arguments, only getting methodname 其中一个是/obj.methodname([\\w,]*)/.exec(text)但没有获取参数,只获取methodname

please help 请帮忙

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 此外,您可以trim值以消除空格

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM