I am trying to split a string by a single or multiple occurrence of letters.
For example:
aaabbcapppp
, would yield the array, ["aaa", "bb", "c", "a", "pppp"]
The most-Inefficient idea I had was to just utilize, newArray = str.split("");
and rebuild the array to my needs. I assume there is a much more efficient solution.
Something like this would work:
"aaabbcapppp".match(/(.)\1*/g) // ["aaa", "bb", "c", "a", "pppp"]
The (.)
matches any single character, captured in group 1, followed by that same character repeated zero or more times (\1
is a backreference which matches exactly what was matched in group 1).
To match only Latin letters, consider using [a-z]
, for example:
"aaa-bbca!!pppp".match(/([a-z])\1*/g) // ["aaa", "bb", "c", "a", "pppp"]
Here, the -
and !!
are not included in the result array.
The regex solution is probably the way to go, but if for some reason you want to do it manually, something like this would work
function charSplit(str) {
var arr = [], l, j = -1;
for (var i=0; i<str.length; i++) {
var c = str.charAt(i);
l==c ? arr[j] += c : arr[++j] = c;
l=c;
}
return arr;
}
FIDDLE