您没有说出您期望的确切输出是什么,但我想像这样的输出可能很直观。
Given:
var myvar = "1-4:2;5-9:1.89;10-24:1.79;25-99:1.69;100-149:1.59;150-199:1.49;200-249:1.39;250+:1.29";
捕获所有子匹配的快速方法是:
var matches = [];
myvar.replace(/(\d+)[-+](\d*):(\d+\.?\d*);?/g, function(m, a, b, c) {
matches.push([a, b, c])
});
(注意:您可以使用[可能更具可读性]循环捕获相同的输出):
var myreg = /(\d+)[-+](\d*):(\d+\.?\d*);?/g;
var matches = [];
while(myreg.exec(myvar)) {
matches.push([RegExp.$1, RegExp.$2, RegExp.$3])
}
无论哪种方式,结果都是一系列匹配:
matches[0]; // ["1", "4", "2"]
matches[1]; // ["5", "9", "1.89"]
matches[2]; // ["10", "24", "1.79"]
matches[3]; // ["25", "99", "1.69"]
matches[4]; // ["100", "149", "1.59"]
matches[5]; // ["150", "199", "1.49"]
matches[6]; // ["200", "249", "1.39"]
matches[7]; // ["250", "", "1.29"]