Skip to content

Instantly share code, notes, and snippets.

@HaiBV
Last active December 12, 2019 15:02
Show Gist options
  • Save HaiBV/1a63711dbd179716b5f4e426b8a1325e to your computer and use it in GitHub Desktop.
Save HaiBV/1a63711dbd179716b5f4e426b8a1325e to your computer and use it in GitHub Desktop.
split string in to n-char long tokens
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