var words = "AAAAAAA BBBBB CCCCC DDDDD\tEEEE \t FFFF".split(/\s+/);
一旦你的話,你可以在單詞重複和推每個字到一個新的數組,如下所示:
編輯#2
var words = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vulputate, velit et luctus tristique, libero quam ornare ligula, sagittis consequat massa tellus vel nulla.".split(/\s+/);
var output = [words[0]];
for (var i = 1; i < words.length; i++) {
var word = words[i];
var prev = output.pop();
if ((prev + " " + word).length <= 10) {
output.push(prev + " " + word);
} else {
output.push(prev);
output.push(word);
}
}
output;
// ["Lorem", "ipsum", "dolor sit", "amet,", "consectetur", "adipiscing", "elit. Sed", "vulputate,", "velit et", "luctus", "tristique,", "libero", "quam", "ornare", "ligula,", "sagittis", "consequat", "massa", "tellus vel", "nulla."]
你的意思是不再話會被瓜分,或從最終陣列排除? – 2011-05-30 07:41:29
是的,更長的單詞將被切片,但總是由單詞之間的空格分開,數組中每個元素的總長度不應超過10個字符長。 – Leem 2011-05-30 07:42:20
拆分是實現您想要的正確方法 - 出了什麼問題? – 2011-05-30 07:45:11