我用这个提取字数:
var sim = /\s+/gi;
var words= parag.value.trim().replace(sim, ' ').split(' ').length;
我想表达重复的话,我怎么做?
答案 0 :(得分:2)
以下代码段会在文本中为您提供一系列重复的字词:
var text = "this is this and that was that";
var frequency = text.split(' ').reduce(function(previous, current) {
if (!previous.hasOwnProperty(current)) {
previous[current] = 0;
}
previous[current] += 1;
return previous;
}, {});
var repeatedWords = Object.keys(frequency).filter(function(element) {
return frequency[element] > 1;
});
console.log(repeatedWords);
// => ["this", "that"]