简体   繁体   中英

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

What I'd like to achieve is splitting a string like this, ie 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'm manually iterating over each character, and I'm appending to the array character by character
  • It's a little too long for the simple thing I want to achieve

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM