You can definitely do something like
let pieces = "1234567890 ".split(/(.{2})/).filter(x => x.length == 2);
to get this:
[ '12', '34', '56', '78', '90' ]
If you want to dynamically input/adjust the chunk size so that the chunks are of size n, you can do this:
n = 2;
let pieces = "1234567890 ".split(new RegExp("(.{"+n.toString()+"})")).filter(x => x.length == n);
To find all possible size n chunks in the original string, try this:
let subs = new Set();
let n = 2;
let str = "1234567890 ";
let regex = new RegExp("(.{"+n.toString()+"})"); //set up regex expression dynamically encoded with n
for (let i = 0; i < n; i++){ //starting from all possible offsets from position 0 in the string
let pieces = str.split(regex).filter(x => x.length == n); //divide the string into chunks of size n...
for (let p of pieces) //...and add the chunks to the set
subs.add(p);
str = str.substr(1); //shift the string reading frame
}
You should end up with:
[ '12', '23', '34', '45', '56', '67', '78', '89', '90', '0 ' ]