pimvdb
pimvdb

Reputation: 154818

How to split a string by a difference in character as delimiter?

What I'd like to achieve is splitting a string like this, i.e. the delimiters are the indexes where the character before that index is different from the character after that index:

"AAABBCCCCDEEE" -> ["AAA", "BB", "CCCC", "D", "EEE"]

I've been trying to make up a concise solution, but I ended up with this rather verbose code: http://jsfiddle.net/b39aM/1/.

var arr = [],               // output
    text = "AAABBCCCCDEEE", // input
    current;

for(var i = 0; i < text.length; i++) {
    var char = text[i];

    if(char !== current) { // new letter
        arr.push(char);    // create new array element
        current = char;    // update current
    } else {                         // current letter continued
        arr[arr.length - 1] += char; // append letter to last element
    }
}

It's naive and I don't like it:

I was thinking of using a regexp but I'm not sure what the regexp should be. Is it possible to define a regexp that means "one character and a different character following"?

Or more generally, is there a more elegant solution for achieving this splitting method?

Upvotes: 3

Views: 267

Answers (1)

Gumbo
Gumbo

Reputation: 655189

Yes, you can use a regular expression:

"AAABBCCCCDEEE".match(/(.)\1*/g)

Here . will match any character and \1* will match any following characters that are the same as the formerly matched one. And with a global match you’ll get all matching sequences.

Upvotes: 6

Related Questions