Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function wordwrap( str, width, brk, cut ) {
brk = brk || '\n';
width = width || 75;
cut = cut || false;
if (!str) { return str; }
var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
matches = str.match( new RegExp(regex, 'g') );
for (i = 0; i < matches.length; i++) {
matches[i] = matches[i].trim();
}
return matches.join( brk );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment