1

The task is to find, whether there are, any series of ones (i.e. "111") and output for each match:

  1. Position of match in input;
  2. Number of successive ones (i.e. for "111" it will be 2);

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:

  1. position as input.indexOf(mg[i][0]);
  2. number of successive digits as mg[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".

3
  • There is no such function in JS. You will need to loop until it yields null. Commented Mar 24, 2016 at 22:54
  • 1
    Do you need something like this? Commented Mar 24, 2016 at 22:58
  • @WiktorStribiżew: Sort of. Thanks. Commented Mar 24, 2016 at 23:00

2 Answers 2

1

You can use the exec method of the String. exec returns an object with an index property:

var str = '111001111';
var re = /(1+)/g;
while ((match = re.exec(str)) != null) {
    console.log(match[0]); // capture group
    console.log(match[0].length); // length
    console.log(match.index); // start index position
}
Sign up to request clarification or add additional context in comments.

Comments

0

You may use RegExp#exec() to keep all captured texts in a match data object.

Here is a sample demo getting:

  • all matches (just the 0th element in the match data object, m[0])
  • all indices (last re index minus the match length, re.lastIndex - m[0].length)
  • length of the successive digits (the length of the first capture group value, m[1].length)

You may revamp it later as per your needs.

var s = "11110110"; 
var re = /1(1+)/g;
var pos = [], numOfSucDigits = [], matches = [];
while ((m=re.exec(s)) !== null) {
  matches.push(m[0]);
  numOfSucDigits.push(m[1].length);
  pos.push(re.lastIndex - m[0].length);
}
document.body.innerHTML = "Matches: " + JSON.stringify(matches) + "<br/>";
document.body.innerHTML += "Count of SD: " + JSON.stringify(numOfSucDigits) + "<br/>";
document.body.innerHTML += "Indices: " + JSON.stringify(pos);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.