最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Regex: How to split Regex subexpression matches in to multi-dimensional string arrays? - Stack Overflow

programmeradmin4浏览0评论

How would you split Regex subexpression matches in to multi-dimensional string arrays?

I have a "myvar" string of:

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

which is a repeat of QuantityLow - QuantityHigh : PriceEach ;

I used this "myreg" Regex /(\d+)[-+](\d*):(\d+\.?\d*);?/g

Used it with var myarray = myvar.match(myreg);

that produced:

myarray[0] = "1-4:2;"
myarray[1] = "5-9:1.89;"
myarray[2] = "10-24:1.79;"
myarray[3] = "25-99:1.69;"
myarray[4] = "100-149:1.59;"
myarray[5] = "150-199:1.49;"
myarray[6] = "200-249:1.39;"
myarray[7] = "250+:1.29"

Fantastic! Except that I need the strings broken further by the Q1 - Q2 : P as noted above. The regex is already setup to identify the parts with parenthesis. I would think this could be done with a single Regex expression, or at least two, rather than setting up some sort of loop.

Thanks for the feedback.

How would you split Regex subexpression matches in to multi-dimensional string arrays?

I have a "myvar" string of:

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

which is a repeat of QuantityLow - QuantityHigh : PriceEach ;

I used this "myreg" Regex /(\d+)[-+](\d*):(\d+\.?\d*);?/g

Used it with var myarray = myvar.match(myreg);

that produced:

myarray[0] = "1-4:2;"
myarray[1] = "5-9:1.89;"
myarray[2] = "10-24:1.79;"
myarray[3] = "25-99:1.69;"
myarray[4] = "100-149:1.59;"
myarray[5] = "150-199:1.49;"
myarray[6] = "200-249:1.39;"
myarray[7] = "250+:1.29"

Fantastic! Except that I need the strings broken further by the Q1 - Q2 : P as noted above. The regex is already setup to identify the parts with parenthesis. I would think this could be done with a single Regex expression, or at least two, rather than setting up some sort of loop.

Thanks for the feedback.

Share Improve this question edited Oct 18, 2009 at 19:43 Peter Boughton 112k32 gold badges123 silver badges177 bronze badges asked Oct 15, 2009 at 2:54 Zachary ScottZachary Scott 21.2k35 gold badges124 silver badges208 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

You didn't say what the exact output you expect, but I imagine something like this output may be intuitive.

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";

A quick way to capture all sub-matches is:

var matches = [];
myvar.replace(/(\d+)[-+](\d*):(\d+\.?\d*);?/g, function(m, a, b, c) {
    matches.push([a, b, c])
});

(Note: you can capture the same output with a [potentially more readable] loop):

var myreg = /(\d+)[-+](\d*):(\d+\.?\d*);?/g;
var matches = [];
while(myreg.exec(myvar)) {
    matches.push([RegExp.$1, RegExp.$2, RegExp.$3])
}

Either way, the oute is an array of matches:

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"]
var r = /(\d+)[-+](\d*):(\d+\.?\d*);?/g;
var s = "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 match;
while ((match = r.exec(s)) != null) {
  console.log("From " + match[1] + " to " + match[2] + " is " + match[3]);
}

The console.log() call is a Firefox/Firebug call. You can of course use alert(), document.write() or whatever else suits.

See RegExp object reference, in particular the RegExp methods, most notably RegExp.exec().

The simpliest way to do it - using split method of String:

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";
myvar.split(';');

that produced:

["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"]

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论