如何使用正则表达式和javascript提取函数参数

时间:2016-11-01 16:17:32

标签: javascript jquery

如果我在页面的标记中有以下内容:

cmCreatePageviewTag("Category: View All Mens (29103)", "29103", null, null, "10151");

如何在括号之间提取参数,以便将以下内容存储在变量中:

"Category: View All Mens (29103)", "29103", null, null, "10151"

1 个答案:

答案 0 :(得分:-1)

var regExp = /[a-zA-Z0-9_]+\((.+)\);/;
var str = 'cmCreatePageviewTag("Category: View All Mens (29103)", "29103", null, null, "10151");';

var details = regExp.exec(str);

console.log(details[1]) // "Category: View All Mens (29103)", "29103", null, null, "10151"

正则表达式搜索任何字母数字字符或下划线(函数名称),后跟(,后跟任意数量的字符(参数),后跟);