There are such lines (or an array of objects) of 96 characters:

1. 000000000000000000000011111111111111100000000000000000000000000000111111111111111111111111111111 2. 000000000000000000000000000000001111111111111111000000001111111111111111111111110000000000000000 3. 000000000000000000000000000000001111111111111111111111111111110000000222222222222222200000000000 

There can be only 3 characters: 0, 1, 2, I need to find the number and position of each group of characters, for example:

 var str = '000000000000000000000000000000001111111111111111111111111111110000000222222222222222200000000000'; 

What should happen:

 zero: [ { startPos: 0, count: 33 }, { startPos: 63, count: 7 }, { startPos: 86, count: 11 } ], one: [ { startPos: 33, count: 30 } ], two: [ { startPos: 70, count: 16 } ] 

1 answer 1

 var s = "000000000000000000000000000000001111111111111111111111111111110000000222222222222222200000000000" var names = ['zero', 'one', 'two'] var res = {} names.forEach(n => res[n] = []) s.replace(/(.)\1*/g, (m,ch,i) => res[names[ch]].push({startPos: i, count: m.length})) console.log(JSON.stringify(res, null, ' '))