I've been attempting to achieve a similar task as someone else, who accomplished it using Ruby while I'm striving to do so with Javascript:
Split a string into an array based on runs of contiguous characters
The objective is to split a single string of characters into an array of contiguous characters. For instance:
Given the input string:
'aaaabbbbczzxxxhhnnppp'
It should result in an array like this:
['aaaa', 'bbbb', 'c', 'zz', 'xxx', 'hh', 'nn', 'ppp']
My current attempt looks something like:
var matches = 'aaaabbbbczzxxxhhnnppp'.split(/((.)\2*)/g);
for (var i = 1; i+3 <= matches.length; i += 3) {
alert(matches[i]);
}
Although it somewhat works, there are still inaccuracies. I find myself splitting too much and having to manipulate the index to filter out unwanted entries.
How can I obtain a clean array with only the desired content?
Appreciate your help!