const splitNchars = (str, n) => str.split(new RegExp(`(.{${~~n}})`)).filter(e => e)
splitNchars("abcdefg", 2)
// ["ab", "cd", "ef", "g"]
文字列を N 文字ごとに改行したい
splitNchars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 13).join("\n")
// abcdefghijklm
// nopqrstuvwxyz
// ABCDEFGHIJKLM
// NOPQRSTUVWXYZ
// 1234567890
文字列を N 個に分割したい
const splitNstrings = (str, n) => {
const arr = splitNchars(str, Math.ceil(str.length / n))
return [...arr, ...Array(n - arr.length).fill("")]
}
splitNstrings("abcdefg", 2)
// ["abcd", "efg"]