Last active
December 12, 2019 15:02
-
-
Save HaiBV/1a63711dbd179716b5f4e426b8a1325e to your computer and use it in GitHub Desktop.
split string in to n-char long tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function split(input, len) { | |
return input.match(new RegExp('.{1,' + len + '}(?=(.{' + len + '})+(?!.))|.{1,' + len + '}$', 'g')) | |
} | |
console.log(split('11010101101', 4)) // ['110', '1010', '1101'] | |
console.log(split('ab22883b0ada0', 2)) // ['a', 'b2', '28', '83', 'b0', 'ad', 'a0'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment