正则表达式匹配功能的parmaters

时间:2016-09-23 14:32:24

标签: javascript regex

我的页面上有以下文字:

pageTracker._addItem("2040504","JACQXSPINKASS-TX4-8","Jacq Socks","","9.00000","1.0");
pageTracker._addItem("2040504","FTWCLSNOCOLOURONE SIZE","Footwear Cleaner","","8.00000","1.0");

我想使用javascripts match()函数提取每行的括号内的参数。我有以下正则表达式,但它不太正确:

/\b_addItem[^);]+/g

这也与_addItem(部分相匹配。我怎样才能调整它以仅获取括号内的内容?

Regexr example

理想情况下,它应匹配任何以pageTracker._addItem("开头的字符串,但不包括匹配结束括号中的那部分。

我将使用javascript进行匹配我不认为支持看起来如果我是对的

3 个答案:

答案 0 :(得分:1)

使用后面的来断言但不捕获前面的文字:

/(?<=pageTracker\._addItem\()[^);]+/g

请注意,我在后面添加了(以便不捕获它。

现在您已经添加了JavaScript代码,但不支持外观,您必须在群组中捕获目标:

/pageTracker\._addItem\(([^);]+)/g

您的目标将在第1组。

答案 1 :(得分:0)

您可以将其拆分为两个正则表达式调用以避免后视:

var str = 'pageTracker._addItem("2040504","JACQXSPINKASS-TX4-8","Jacq Socks","","9.00000","1.0");\npageTracker._addItem("2040504","FTWCLSNOCOLOURONE SIZE","Footwear Cleaner","","8.00000","1.0");'
var m,output=[];
var re = /^pageTracker._addItem\("(.*)"\)/gm;
while(m=re.exec(str)) 
  output.push(m[1].split('","'));

然后输出为2D数组:

[ 
  ["2040504","JACQXSPINKASS-TX4-8","Jacq Socks","","9.00000","1.0"],
  ["2040504","FTWCLSNOCOLOURONE SIZE","Footwear Cleaner","","8.00000","1.0"]
]

答案 2 :(得分:0)

你也可以这样做

&#13;
&#13;
var strings = ['pageTracker._addItem("2040504","JACQXSPINKASS-TX4-8","Jacq Socks","","9.00000","1.0");',
               'pageTracker._addItem("2040504","FTWCLSNOCOLOURONE SIZE","Footwear Cleaner","","8.00000","1.0");'
              ],
       args = strings.map(s => s.match(/".*?"/g)
                                .map(s => s.replace(/"/g,'')));
console.log(args);
&#13;
&#13;
&#13;