Split a string to groups of 2 chars using split?

后端 未结 3 798
长情又很酷
长情又很酷 2020-12-10 04:19

I have this simple string :

\"a1a2a3\"

Is there any regex expression which can be used with split command so it will split the str

3条回答
  •  萌比男神i
    2020-12-10 05:04

    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,}/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.

提交回复
热议问题