s = "aaa bbb ccc ddd eee fff ggg hhhh iii"; var a= s.split(" "); var t = "" for(var i=0;i<a.length;i++){ t += a[i] + " "; if(i%3==2) t+="<br/>" } alert(t)
如果单词间的两个或两个以上的空格也算作一个空格的话:window.onload = function() { var s = 'The book provides \ complete, accurate, and \ detailed coverage of the \ Java programming language. \ It provides full coverage of all \ new features added in since the \ previous edition including generics, \ annotations, asserts, autoboxing, \ enums, for each loops, variable arity \ methods and static import clauses.'; var r = s.replace(/((?:[^ ]+ +){2})([^ ]+) +/g, '$1$2<br />'); alert(r); }
string x = "aaa bbb ccc ddd eee fff ggg";var i = 0;var l = 9,n=""; while(i<x.length){ i += l; n += x.substr(i,l) + "<br />"; }return n;
s = "aaa bbb ccc ddd eee fff ggg hhhh iii";
var a= s.split(" ");
var t = ""
for(var i=0;i<a.length;i++){
t += a[i] + " ";
if(i%3==2) t+="<br/>"
}
alert(t)
var s = 'The book provides \
complete, accurate, and \
detailed coverage of the \
Java programming language. \
It provides full coverage of all \
new features added in since the \
previous edition including generics, \
annotations, asserts, autoboxing, \
enums, for each loops, variable arity \
methods and static import clauses.'; var r = s.replace(/((?:[^ ]+ +){2})([^ ]+) +/g, '$1$2<br />');
alert(r);
}
while(i<x.length){
i += l;
n += x.substr(i,l) + "<br />";
}return n;
var str = "aaa bbb ccc ddd eee fff ggg hhhsfdja hifjds fds";
str = str.replace(/(\w+\s+)(\w+\s+)(\w+)\s+/g, "$1$2$3<br/>");
alert(str);