Hello friend searching the internet I found this function:
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|$)');
return str.match( RegExp(regex, 'g') ).join( brk );
}
Usage:
wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br/>\n');
Result:
The quick brown fox
jumped over the lazy
dog.
Function with concatenation of start and end:
function wordwrap( str, width, brk) {
brk = brk || '\n';
width = width || 75;
if (!str) { return str; }
var regex = '.{1,' +width+ '}(\s|$)' + '|\S+?(\s|$)';
var array = str.match( RegExp(regex, 'g') );
var frase = "";
for (var i = 0; i < array.length; i++) {
console.log(array[i]);
frase += i + " " + array[i] + "\n";
};
return frase;
}
alert(wordwrap('The quick brown fox jumped over the lazy dog.', 30));