You can use a regular expression, for example /\S+/g , to split a line if the words are separated by multiple spaces or any other spaces.
I'm not sure my example below is the most elegant way to do this, but it works.
<html> <head> <script type="text/javascript"> var str = "one two three four five six seven eight nine ten " + "eleven twelve thirteen fourteen fifteen sixteen " + "seventeen eighteen nineteen twenty twenty-one"; var words = str.match(/\S+/g); var arr = []; var temp = []; for(var i=0;i<words.length;i++) { temp.push(words[i]); if (i % 10 == 9) { arr.push(temp.join(" ")); temp = []; } } if (temp.length) { arr.push(temp.join(" ")); } </script> </head> <body> </body> </html>
source share