Is there a way to split a JavaScript string into an array of strings of a specified length, where the length can vary? I need to have the length parameter as a separate variable:
var length = 3;
var string = 'aaabbbcccddd';
var myArray = string.match(new RegExp('.{' + length + '}', 'g'));
How can I use the length variable in the match function? Or is there any other solution similar to str_split in PHP?
My question is different from: Split large string in n-size chunks in JavaScript because I already know how to slice, but I want to know how to use a variable in the match function.
I am struggling to achieve this, as mentioned inJavascript Regex: How to put a variable inside a regular expression?