Split a string to groups of 2 chars using split?

I already answered this in chat. You really should be using String.prototype.match.

Can be done with split? Technically, yes. You can split it by /(..)/ and then filter out the blank strings.

"a1a2a3".split(/(..)/).filter(function(a){
    return a !== '';
});

split for even length string:

str.split(/(?=(?:..)*$)/)

split for odd length string, the last entry has single character:

str.split(/(?=(?:..)*.$)/)

Those are basically look-aheads that check whether the number of characters ahead is odd or even. It takes advantage of the fact that the number of characters ahead at all the split positions have the same parity as the length of the string.

The pattern in the (even version) look-ahead is (?:..)*$, which checks for even number of characters (?:..)* before the end of the string $. (Note that non-capturing group (?:pattern) is used here, otherwise, capturing group will create extra entries in the split result). Similar explanation applies for the odd version.

Note that . excludes several new line characters: \n, \r, \u2028 or \u2029. It will produce unexpected result for string containing such characters. Replace . with [\s\S] (or other equivalent construct) to make it works for all cases.


For practical purpose, match is the right tool for the job:

str.match(/..?/g)

For example:

"1234567890".match(/..?/g)
> [ "12", "34", "56", "78", "90" ]

"1234567890".match(/..?/g)
> [ "12", "34", "56", "78", "9" ]

The solution can be extended to group of n characters:

str.match(/.{1,<n>}/g)

For example:

"123456789012345678901234567890".match(/.{1,7}/g)
> [ "1234567", "8901234", "5678901", "2345678", "90" ]

It simply takes advantage of the greedy quantifier and creates groups of n characters, before running out of characters to match for the last group.

Same as above, you may want to change . to [\s\S] to make it work for all cases.