如何将双空格或三空格句子逐字拆分成数组Node.js?
How split Double or Triple Spaces Sentences word by word into Array Node.js?
我是节点的新手 js.I 我很困惑如何通过删除作者姓名来拆分这些句子并按字数将引号放入单个数组中。
https://raw.githubusercontent.com/alvations/Quotables/master/author-quote.txt
我想做这个图案
{
quotes: 'hello world',
wordCount: 2
}
我的尝试
const readline = require('linebyline'),
rl = readline('./data.txt');
rl.on('line', function(line, lineCount, byteCount) {
let lineSplit = line.split(" ");
// I don't what to do
}).on('error', function(e) {
console.log(e,"ERROR")
});
var a = "A. A. Milne If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you."
a = a.substring(a.indexOf("\t") + 1);
//gives
//"If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you."
a.trim().split(/\s+/).length
// gives you the word count (29)
所以你的代码应该是:
const readline = require('linebyline'),
rl = readline('./data.txt');
var finalResult = [];
rl.on('line', function(line, lineCount, byteCount) {
line = line.substring(line.indexOf("\t") + 1);
finalResult.push({
quotes: line,
wordcount: line.trim().split(/\s+/).length
});
}).on('error', function(e) {
console.log(e,"ERROR")
});
我使用了 npm 包 https://www.npmjs.com/package/line-by-line
,但你可以使用任何其他包
const readline = require('line-by-line')
const rl = new readline('author-quote.txt')
let data = []
rl.on('line', function (line) {
let sentence = line.substring(line.indexOf("\t") + 1)
data.push({
count: sentence.split(' ').length,
quote: sentence
})
})
rl.on('end', function() {
console.log(data) // or write to file
})
改进并减少了一点代码:
var a = "A. A. Milne If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you."
a = a.substring(a.indexOf("\t") + 1);
console.log(a.split(/[\s\t\n\r]+/));
\s is the escape for space
\t is the escape for tab
\n is the escape for line feed
\r is the escape for carriage return
const readline = require('linebyline'),
rl = readline('./data.txt');
var finalResult = [];
rl.on('line', function(line, lineCount, byteCount) {
line = line.substring(line.indexOf("\t") + 1);
finalResult.push({
quotes: line,
wordcount: line.split(/[\s\t\n\r]+/).length
});
}).on('error', function(e) {
console.log(e,"ERROR")
});
如果作者不是由引号分隔的制表符,您将无法识别。
$(document).ready(() => {
$.ajax({
url: "https://raw.githubusercontent.com/alvations/Quotables/master/author-quote.txt",
success: (response) => {
var lines = response.split(/\n/),
fn = function() {
var line = lines.pop();
if (!line) return;
var t = line.indexOf("\t");
if (t != -1) {
line = line.substring(t + 1);
console.log(lines.length, ' OK');
//console.log(line, "(", line.split(/[\t\s\n\r]+/).length,")");
} else {
console.log(lines.length, ' NOT OK', '***', line, '***');
}
};
var id = setInterval(function() {
if (lines.length) {
fn();
} else {
clearInterval(id);
console.log('DONE!');
}
}, 2);
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我是节点的新手 js.I 我很困惑如何通过删除作者姓名来拆分这些句子并按字数将引号放入单个数组中。
https://raw.githubusercontent.com/alvations/Quotables/master/author-quote.txt
我想做这个图案
{
quotes: 'hello world',
wordCount: 2
}
我的尝试
const readline = require('linebyline'),
rl = readline('./data.txt');
rl.on('line', function(line, lineCount, byteCount) {
let lineSplit = line.split(" ");
// I don't what to do
}).on('error', function(e) {
console.log(e,"ERROR")
});
var a = "A. A. Milne If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you."
a = a.substring(a.indexOf("\t") + 1);
//gives
//"If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you."
a.trim().split(/\s+/).length
// gives you the word count (29)
所以你的代码应该是:
const readline = require('linebyline'),
rl = readline('./data.txt');
var finalResult = [];
rl.on('line', function(line, lineCount, byteCount) {
line = line.substring(line.indexOf("\t") + 1);
finalResult.push({
quotes: line,
wordcount: line.trim().split(/\s+/).length
});
}).on('error', function(e) {
console.log(e,"ERROR")
});
我使用了 npm 包 https://www.npmjs.com/package/line-by-line
,但你可以使用任何其他包
const readline = require('line-by-line')
const rl = new readline('author-quote.txt')
let data = []
rl.on('line', function (line) {
let sentence = line.substring(line.indexOf("\t") + 1)
data.push({
count: sentence.split(' ').length,
quote: sentence
})
})
rl.on('end', function() {
console.log(data) // or write to file
})
改进并减少了一点代码:
var a = "A. A. Milne If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you."
a = a.substring(a.indexOf("\t") + 1);
console.log(a.split(/[\s\t\n\r]+/));
\s is the escape for space
\t is the escape for tab
\n is the escape for line feed
\r is the escape for carriage return
const readline = require('linebyline'),
rl = readline('./data.txt');
var finalResult = [];
rl.on('line', function(line, lineCount, byteCount) {
line = line.substring(line.indexOf("\t") + 1);
finalResult.push({
quotes: line,
wordcount: line.split(/[\s\t\n\r]+/).length
});
}).on('error', function(e) {
console.log(e,"ERROR")
});
如果作者不是由引号分隔的制表符,您将无法识别。
$(document).ready(() => {
$.ajax({
url: "https://raw.githubusercontent.com/alvations/Quotables/master/author-quote.txt",
success: (response) => {
var lines = response.split(/\n/),
fn = function() {
var line = lines.pop();
if (!line) return;
var t = line.indexOf("\t");
if (t != -1) {
line = line.substring(t + 1);
console.log(lines.length, ' OK');
//console.log(line, "(", line.split(/[\t\s\n\r]+/).length,")");
} else {
console.log(lines.length, ' NOT OK', '***', line, '***');
}
};
var id = setInterval(function() {
if (lines.length) {
fn();
} else {
clearInterval(id);
console.log('DONE!');
}
}, 2);
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>