Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // wrap(text[, width=78[, br='\n']])
- // Работает так же как wordwrap в PHP.
- // @author Сирожа <tz4678@gmail.com>
- function wrap(text, width, br) {
- var lines, i, re, line, buf, matches, whitespace, word;
- width = arguments.length > 1 ? arguments[1] : 78;
- br = arguments.length > 2 ? arguments[2] : '\n';
- lines = text.split('\n');
- for (i = 0; i < lines.length; ++i) {
- re = /(\s*)(\S+)/g;
- line = [];
- buf = '';
- while (matches = re.exec(lines[i])) {
- whitespace = matches[1];
- word = matches[2];
- if (buf.length + whitespace.length + word.length <= width) {
- buf += whitespace;
- }
- else {
- line.push(buf);
- buf = '';
- }
- buf += word;
- }
- line.push(buf);
- lines[i] = line.join(br);
- }
- return lines.join('\n');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement