1

我需要查找 td.aws 中的字符串是否出现超过 3 次,如果出现,则将该字符串放入新列表中。

我有一张这样的桌子:

<table width="100%" cellspacing="0" cellpadding="2" border="1" class="aws_data">
<tbody><tr bgcolor="#ECECEC"><th>URL (1,908)</th></tr>
<tr><td class="aws">/images/bullet3.png</td></tr>
<tr><td class="aws">/pdf-signing-tool/ErrorCode.properties</td></tr>
<tr><td class="aws">/pdf-signing-tool/Display.properties</td></tr>
<tr><td class="aws">/evcert.cfm</td></tr>
<tr><td class="aws">/evcert.cfm</td></tr>
<tr><td class="aws">/evcert.cfm</td></tr>
<tr><td class="aws">/evcert.cfm</td></tr>
<tr><td class="aws">/repository/03</td></tr>
<tr><td class="aws">/repository/0</td></tr>
etc

<div id="problems"></div>

到目前为止,我有:

$('.aws').each(function(){
var temp = $(this).text();
var count = temp.match('/'+temp+'/g');  

if (count.length > 3)
{
    thisString = $(this).text();
    $('#problems').append(thisString)
}

});

谁能帮忙,目前我只是收到 JS 错误“计数为空”

JS小提琴

4

4 回答 4

2

例子

//store the counts for each "text" occurrence in a hash table
var countHash = {}; 

//iterate over your tds
$('.aws').each(function(){

    //pull of the text
    var temp = $(this).text();

    //has it already been added to the list? 
    //see: 'countHash[temp] = false;' below.
    if(countHash[temp] === false){return;}

    //increment the occurrence count
    //or set to 1 if this is the first occurrence.
    countHash[temp] = (countHash[temp] || 0) + 1; 

    //have more than three been found?
    if (countHash[temp] > 3)
    {
        //add to your list
        $('#problems').append(temp);

        //ignore future occurrences
        countHash[temp] = false; 
    }
});
于 2013-04-12T12:24:21.437 回答
2

new RegExp()如评论中所述,应该使用创建这样的正则表达式。除此之外,如果使用括号和问号之类的东西,使用每个表格单元格的内部文本可能会导致无效的正则表达式。因此,在这种情况下,我建议不要这样做。

您可以在遍历 each 时像这样进行频率计数td.aws

var frequencies = {};

$('td.aws').each(function() {
    var key = $(this).text(),
    freq = frequencies[key] || 0;

    // increase the frequency and check if it goes above 3
    if (++freq > 3) {
        $('#problems').append(key);
        freq = -Infinity;
    }

    frequencies[key] = freq;
});

演示

对象frequencies在其属性中保留每个术语的频率;一旦达到一定数量,它就会做任何你需要的事情。

于 2013-04-12T12:29:05.070 回答
1
var count = {};
$(".aws").each(function(i,v) {
    var temp = $(v).text();
    var current = count[temp];
    if (!current) {
        current = 0;
    }
    current++;
    count[temp] = current;
    if (current > 3) {
       $("#problems").append("<p>"+temp+"</p>");
    }
}

我不确定您是否要从旧列表中删除出现三次的项目,所以我没有添加

于 2013-04-12T12:19:53.780 回答
1

您的问题在这里:'/'+temp+'/g'您不能在正则表达式文字中使用变量。您必须将正则表达式构建为字符串:

var tempRegex = new RegExp(temp, 'g');
var count = temp.match(tempRegex);

您的代码中似乎存在更大的逻辑问题。现在你正在构建一个正则表达式来查看文本是否匹配自身,这总是会的。我认为您正在尝试搜索所有 TD 以确定是否存在重复项。试试这种方法:

var items = [];

$('.aws').each(function(){
    var currentText = $(this).text();
    var duplicate = false;

    for (var i = 0; i < items.length; i++) {
        if (items[i].text === currentText) {
            items[i].count++;

            if (items[i].count > 2) {
                console.log('found more than 2 of ' + currentText);
                $('#problems').append(currentText)
            }

            duplicate = true;
            break;
        }
    }

    if (!duplicate) {
        items.push({ text: currentText, count: 1});
    }
});

http://jsfiddle.net/qT6Nz/2/

于 2013-04-12T12:24:32.077 回答