1

I have a textarea with unfiltered user input, which includes line returns, spaces, punctuation marks, etc. I would like to get all the distinct lowercased words, their occurrence, sorted by occurrence. I haven't found a straight forward way to extract words when the strip() string is variable. Any ideas how to achieve this?

For example:

WORD1 Word2 word1 Word1, ...
word2 HELLO ...
. . hello .hi

would become

val array = {
    word1 : 3,
    word2 : 2,
    hello : 2,
       hi : 1
};

Thanks for your help!

4

2 に答える 2

3

考えられる解決策の1つは次のとおりです。

var result = {},
    value = $("textarea").val(),
    res = value.match(/\b([a-z0-9]+)\b/g) || [];

for (var i = 0; i < res.length; i++) {
    result[res[i]] = (value.match(new RegExp(res[i], "ig")) || []).length;
}

console.log(result);​

デモ:http: //jsfiddle.net/mmFgE/

于 2012-09-28T12:42:46.197 に答える
1

クイックデモ: 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);
于 2012-09-28T12:43:31.207 に答える