Hello friend searching on the internet 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 );
}
Use:
wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br/>\n');
Upshot:
The Quick Brown fox
jumped over the Lazy
dog.
Function with start and end concatenation:
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));
Excellent, Matheus. It was a regex so needed. Thank you.
– Helio Bentzen
Do you have any idea how I could insert a string at the beginning of each row of that? At the end is easy from the cut parameter, but I did not see a quick way to concatenate in front.
– Helio Bentzen
I made a modification in the function I will publish just below, if you can give me more points so I thank you =D
– Matheus Gonçalves
Friend, I modified the answer by adding the function I modified to concatenate at the beginning and at the end
– Matheus Gonçalves
Thanks, Matheus, for this I ended up applying something like this: <tag>' + wordwrap(phrase, 130, '</tag> n<tag>') + '</tag>'; But your solution is also great. Thanks again.
– Helio Bentzen