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

Snippets has posted 5883 posts at DZone. View Full User Profile

WordWrap //JavaScript Function

  • submit to reddit
        <a href="">



alert("My world is biiiiiiiiiiiiig".wordWrap(10, "\n+", true));
alert("My world is biiiiiiiiiiiiig".wordWrap(10, "\n>", false));


//+ Jonas Raoni Soares Silva
//@ [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");


Snippets Manager replied on Mon, 2012/05/07 - 1:16pm

AAAAAAAAAAAAAAAAAAAAAAAAAA alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n"));

Snippets Manager replied on Mon, 2012/05/07 - 1:16pm

It replaced my br tag aaaaaaa, I won't try to post "& lt ;" (maybe it work: <) xD alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n", true)); alert("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "\n", true));

Snippets Manager replied on Mon, 2012/05/07 - 1:16pm

Hi, I didn't checked comments on my codes for a long time, so I'll answer :) @susan12345 Here it's working fine my code version... document.write("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq".wordWrap(10, "
")); 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 - 4:28am

hi! everybody your "clever but unreadable code" does'nt seem to work for the strings like qqqqqqqqqqqqqqqq...and longer wraps this string like this qqqqqq qqqqqqq qqqqqqqqq ...i.e with spaces...please find a better solution but quickly....

Snippets Manager replied on Sat, 2006/07/15 - 7:58pm

This is very clever, if unreadable. But I think it has a bug in it, since the j.input.length + (j = s.substr(m).match(/^\S*/)).input.length + j[0].length clause is not functionally different than j.input.length + (j = s.substr(m).match(/^\S*/)).input.length as 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 as m + (j = s.substr(m).match(/^\S*/)).input.length sincec 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.