Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 593
user2715877 Avatar asked Feb 24 '14 19:02

user2715877


Video Answer


2 Answers

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

like image 26
adeneo Avatar answered Oct 12 '22 12:10

adeneo


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.

like image 76
p.s.w.g Avatar answered Oct 12 '22 13:10

p.s.w.g