Split a string, at every nth position, with JavaScript? [duplicate]

Try the below code:

var foo = "foofaafoofaafoofaafoofaafoofaa";
console.log( foo.match(/.{1,3}/g) );

For nth position:

foo.match(new RegExp('.{1,' + n + '}', 'g'));

Leave a Comment