The task is to find, whether there are, any series of ones (i.e. "111") and output for each match:
- Position of match in input;
- Number of successive ones (i.e. for
"111"it will be2);
I use following regexps: /1(1+)/g and /1(1+)/; unfortunately, none of them work properly:
var m = "11110110".match(/1(1+)/); // ["1111", "111"]
// `m[0]` is the first match and `m[1]` is a capture group of the first match;
// error: there is no second match
var mg = "11110110".match(/1(1+)/g); // ["1111", "11"]
// `mg[0]` is the first match and `mg[1]` is the second match;
// error: there are no capture groups
In the case of "11110110" input I need something like:
var mg = "11110110".__(/1(1+)/g); // [["1111", "111"], ["11", "1"]]
// `mg[0][0]` is the 1st match, `mg[0][1]` is its capture group,
// `mg[1][0]` is the 2nd match and `mg[1][1]` is its capture group;
Then for each match I define:
positionasinput.indexOf(mg[i][0]);number of successive digitsasmg[i][1].length;
How should I do that? What regexp or method should I use?
Or, maybe, there is a totally different technique, which I'm not familiar with?
P.S.: If I got it right, the question is not duplicate for this one, since it is about "how to", not "why".
null.