How can I make a function that splits a multiline string every nth character. I have the below function, but it doesn't work correctly with multiline strings.
splitIntoChunks("abab", 2) works correctly and gives ["ab", "ab"]
But, this doesn't work correctly:
var str = `aaa
bbb
ccc
ddd`
splitIntoChunks(str, 2)
This returns ["aa", "a", "bb", "b", "cc", "c", "dd", "d"]
And what I need is:
["aa" ["a\n", "bb", "b\n", "cc", "c\n", "dd", "d\n"]]
It needs to keep the \n
function splitIntoChunks(str, size) {
return str.match(new RegExp('.{1,' + size + '}', 'g'));
}