如何将双重或三重空格单词逐句分解为Array Node.js?

我对节点j是新的。我很困惑,如何通过删除作者的名字,并通过单词计数得到单引号引号分裂这些句子。

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") }); 

我使用了nam包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 = "AA 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") }); 

改进和减less代码:

 var a = "AA 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]+/));