DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
WordWrap //JavaScript Function
<a href="http://jsfromhell.com/string/wordwrap">
[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>
usage
alert("My world is biiiiiiiiiiiiig".wordWrap(10, "\n+", true));
alert("My world is biiiiiiiiiiiiig".wordWrap(10, "\n>", false));
code
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/wordwrap [v1.0]
String.prototype.wordWrap = function(m, b, c){
var i, j, l, s, r;
if(m < 1)
return this;
for(i = -1, l = (r = this.split("\n")).length; ++i < l; r[i] += s)
for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : ""))
j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length
|| c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
return r.join("\n");
};






Comments
Snippets Manager replied on Mon, 2012/05/07 - 2:16pm
alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n"));Snippets Manager replied on Mon, 2012/05/07 - 2:16pm
alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n", true)); alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n", true));Snippets Manager replied on Mon, 2012/05/07 - 2:16pm
")); And document.write("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "
", true)); @nkallen If I did all those things, there must be a reason, normally I don't make duplicated code, so that was the reason, test the above 2 lines with your version.
Snippets Manager replied on Wed, 2008/01/09 - 5:28am
Snippets Manager replied on Sat, 2006/07/15 - 8:58pm
j.input.length + (j = s.substr(m).match(/^\S*/)).input.length + j[0].lengthclause is not functionally different thanj.input.length + (j = s.substr(m).match(/^\S*/)).input.lengthas you're substr-ing a string length greater than the string itself. Javascript doesn't complain of course, but this is wrong. Furthermore, this very complicated clause is written more comprehensibly asm + (j = s.substr(m).match(/^\S*/)).input.lengthsincec j.input.length is always just m. This, in turn, is equivalent to s.length In other words, I think this whole clause is a waste of code. On another note, I needed a word wrap function that will wrap at spaces if possible AND break-up any words longer than m. Neither values of c (true or false) as paramters to your function will result in this since although c=true will breakup single words longer than m, it doesn't wrap at word boundaries where this is possible, and will break up words shorter than m. I can't imagine anyone wanting word-wrap functionality along these lines! I rewrote your function to generate the output I want: "My word is biiiiiiiiiiiiiiig" => "My word is\nbiiiiiiiii\niiig" Here is the function:String.prototype.wordWrap = function(m, b, c){ var i, j, s, r = this.split("\n"); if(m > 0) for(i in r){ for(s = r[i], r[i] = ""; s.length > m; j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length || m, r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "") ); r[i] += s; } return r.join("\n"); };I.e., just replace your mysterious clause with m.