快速演示:http: //jsfiddle.net/ZaQqb/
注意:您将需要添加更多特殊字符转换等。
代码:
var t = $('textarea').val();
console.log('Original: ' + t);
// 1. prepare your text
t = t.toLowerCase();
while(t.indexOf('.') != -1) t = t.replace('.', ' ');
while(t.indexOf(',') != -1) t = t.replace(',', ' ');
// TODO: add replcement for more spl characters here
while(t.indexOf(' ') != -1) {
t = t.replace(' ', ' ');
}
console.log('Prepared: ' + t);
// 2. split by ' '
t = t.split(' ');
// 3. count
var counts = {};
for(var i in t) {
counts[t[i]] = (counts[t[i]] == undefined) ? 1 : counts[t[i]]+1;
}
console.log(counts);