If you're looking to easily divide a string into three parts, the following method will do just that.*
function splitIntoChunks(string) {
var regex = RegExp(".{1,"+Math.ceil(string.length/3)+"}", 'g');
return string.match(regex);
}
This code creates a regular expression function specifically designed to break down a string into chunks. The uniqueness here lies in determining chunk length based on dividing the overall string length by 3 (as indicated in the regex
variable).
* However, it's important to note that this method works best for strings longer than 5 characters. Shorter strings may not be evenly divided into 3 chunks due to insufficient length.
Sample outputs include:
> splitIntoChunks("abcdefg");
(3) ["abc", "def", "g"]
> splitIntoChunks("abcdefghijk");
(3) ["abcd", "efgh", "ijk"]
> splitIntoChunks("abc");
(3) ["a", "b", "c"]
> splitIntoChunks("abcd"); // :(
(2) ["ab", "cd"]