我有一个非常简单的问题,但需要一个有效的解决方案。我有一个节点脚本打开一个文本文件,循环遍历每一行,如果超过140个字符但需要尊重字边界,则对该行进行分块。这是我到目前为止,但线条不受影响。我也试过_.invoke(lines, function() { splitText(this); };
,但这也让线条不受影响。任何人都可以提出另一种方法吗?
var args = process.argv.splice(2),
fs = require('fs'),
_ = require('underscore'),
splitText;
splitText = function (textSegment) {
var len = 140, curr = len, prev = 0, output = [], currReverse;
while (textSegment[curr]) {
if (textSegment[curr++] == ' ') {
output.push(textSegment.substring(prev, curr));
prev = curr;
curr += len;
} else {
currReverse = curr;
do {
if (textSegment.substring(currReverse - 1, currReverse) == ' ') {
output.push(textSegment.substring(prev, currReverse));
prev = currReverse;
curr = currReverse + len;
break;
}
currReverse--;
} while (currReverse > prev);
}
}
output.push(textSegment.substring(prev));
return output;
}
text = fs.readFileSync(args[0], 'utf-8');
lines = text.split("\n");
lines = _.filter(lines, function (line) {
return line.length >= 100;
});
lines = _.map(lines, function (line) {
return splitText(line);
});
fs.writeFile(args[0], lines.join("\n"), function (err) {
if (err) throw err;
console.log('test');
});
答案 0 :(得分:2)
我对underscore.js没有任何经验,但我确实知道一种相当简单的方法来解决这个问题:
function formatStr(text, len) {
len = len||140;
var i=0,
str, newline, breakpt,
formatted = '';
while (i+len<text.length) {
str = text.substr(i, len);
newline = str.indexOf('\n');
if (newline!=-1) {
formatted += str.substr(0,newline+1);
console.log(i,newline);
i += newline + 1;
continue;
}
breakpt = str.lastIndexOf(' ');
formatted += str.substr(0,breakpt) + '\n';
i+=breakpt+1;
}
// add last line to the end and return; credit to Charly
// for mentioning this was missing.
return formatted + text.substr(i);
}
这个循环的作用如下:
答案 1 :(得分:0)
有点旧线程,但这是一个快速的解决方案。
尝试使用此正则表达式,您可以在此处查看其工作原理:http://regexper.com/#%5E(%5Cr%5Cn%7C.)%7B1%2C140%7D%5Cb
str.match(/^(\r\n|.){1,140}\b/g).join('')