Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  44] [ 4]  / answers: 1 / hits: 57792  / 12 Years ago, tue, october 2, 2012, 12:00:00

I have the following string:



foofaafoofaafoofaafoofaafoofaa


An array with 10 rows (if I split by every 3rd character, that is), which looks something like this, if I were to instantiate it:



var fooarray = new Array ('foo', 'faa', 'foo', 'faa', 'foo', 'faa', 'foo', 'faa', 'foo', 'faa');


So I want a function, either built-in or custom-made, which can help me split up a string by every nth character.


More From » string

 Answers
58

Try the below code:





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





For nth position:



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

[#82804] Sunday, September 30, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kenyonmasonc

Total Points: 44
Total Questions: 117
Total Answers: 116

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;