split string every nth characters javascript 1

split string every nth characters javascript

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

Here is what the above code is Doing:
1. The regex /.{1,3}/g is looking for any character (.) that occurs 1 to 3 times ({1,3}) and the g flag is telling it to repeat this search globally (g).
2. The string.match() method is looking for matches of the regex in the string and returning them in an array.
3. The result is an array of strings, each of which is a substring of the original string.

Similar Posts