Skip to content

Instantly share code, notes, and snippets.

@devNoiseConsulting
Created March 1, 2018 17:46
Show Gist options
  • Select an option

  • Save devNoiseConsulting/dd88a6a433861cb0098d17802c0ce2d8 to your computer and use it in GitHub Desktop.

Select an option

Save devNoiseConsulting/dd88a6a433861cb0098d17802c0ce2d8 to your computer and use it in GitHub Desktop.
Divide String - PhillyDev Slack #daily_programmer - 20180301
const divideString = function(string, size) {
const regex = new RegExp(`(.{${size}})`);
let chunks = string.split(regex).filter(v => v);
if (chunks[chunks.length - 1].length == size) {
return chunks;
} else {
return [chunks.slice(0, -1), chunks[chunks.length - 1]];
}
console.log(chunks);
};
let test = 'phillydev';
let size = 3;
let result = divideString(test, size);
console.log(result);
size = 2;
result = divideString(test, size);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment