user2715877
user2715877

Reputation: 543

JavaScript Split string by multiple occurrences of letters

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.

Upvotes: 15

Views: 7172

Answers (2)

adeneo
adeneo

Reputation: 318182

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

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149020

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.

Upvotes: 31

Related Questions