betazold
Vi Veri Veniversum Vivus Vici
  1. var wordwrap = function(value, width, separator) {
  2. width = width || 75;
  3. separator = separator || '<br>';
  4. if ((typeof(value) === 'string') && value) {
  5. var main = new RegExp('(\\s*)([^><]{' + width + ',})(<|$)', 'g');
  6. value = value.replace(main, function(full, before, v, after) {
  7. var regex = new RegExp('\\S{' + width + '}', 'g'),
  8. match,
  9. matches = [before],
  10. lastIndex = 0;
  11. while ((match = regex.exec(v)) != null) {
  12. if (lastIndex < match.index) {
  13. matches.push(v.substring(lastIndex, match.index));
  14. }
  15. matches.push(match[0] + separator);
  16. lastIndex = regex.lastIndex;
  17. }
  18. matches.push(v.substring(lastIndex));
  19. matches.push(after);
  20. return matches.join('');
  21. });
  22. }
  23. return value;
  24. };

@темы: JavaScript