我有一个包含以下信息的var:
[[code=666]],[[code=777]],
我想迭代这些并将"666"
和"777"
保存在{"666","777"}
这样的数组中。我尝试了以下内容,但将其保存为{"6","6, "6", "7", "7", "7"}
。
var t = this;
var regex = /\[\[code=.*?\]\]/gi;
var match = t;
match = t.data.u_pricing.match(regex);
var final_code = [];
if (match != null) {
//itereate through the matches eg. [[code=666]] [[code=777]]
for (var i = 0; i < match.length; i++) {
var init_index = match[i].indexOf("=") + 1;
var end_index = match[i].length - 2;
for (init_index; init_index < end_index; init_index++) {
final_code.push(match[i][init_index]);
}
console.log("FINAL CODE" + final_code);
}
}
答案 0 :(得分:0)
你一直在推文。你可以使用slice()作为例子。
var t = this;
var regex = /\[\[code=.*?\]\]/gi;
var match = t;
match = t.data.u_pricing.match(regex);
var final_code = [];
if (match != null) {
console.log(match)
//itereate through the matches eg. [[code=666]] [[code=777]]
for (var i = 0; i < match.length; i++) {
var init_index = match[i].indexOf("=") + 1;
var end_index = match[i].length - 2;
final_code.push(match[i].slice(init_index, end_index))
}
console.log(final_code);
}
答案 1 :(得分:0)
由于安德烈亚斯和巴恩斯基的回答对你不起作用,也许会这样做:
var str = '[[code=666]],[[code=777]]'; // this.data.u_pricing
var regex = /\[\[code=(.*?)\]\]/gi;
var match;
var final_code = [];
while (match = regex.exec(str)) {
final_code.push(match[1]);
}
console.log(final_code);
&#13;
答案 2 :(得分:0)
var test = '[[code=666]],[[code=777]]';
var result = [];
test.split(']]').forEach(function(str) {
if(str.indexOf('[[code=') != -1) {
result.push(str.replace('[[code=', ''));
}
});
console.log(result);