2017-08-26 89 views
0

我已经创建了一个简单的程序,它基于来自外部txt文件的列表,并使用正则表达式在我的js文件中对列表进行过滤。我的过滤器函数没有注册任何与&in JavaScript

大多数情况下,当我从txt文件输入单词列表时,它们被过滤掉。然而,有一个关键字引起我的麻烦。

“黑&德克尔”

我猜测它与“&”的牌子做的,因为任何我投入txt文件用“&”标志的新词不筛选出的某些原因。

任何人都可以帮助我为什么词&没有正确过滤吗?并检查我的正则表达式是否正确写入该程序(var过滤)?

任何帮助将不胜感激。谢谢。

我有一个txt文件在这里的单词列表:

baby bullet, baby-bullet, back2life, back-2-life, black & decker, black-decker, black-&-decker, britax, bose, capital brands products, capital-brands-products, dewalt, dyson, ergobaby, ergo-baby, fiskars, ickle bubba, ickle-bubba, kitchen aid, kitchen-aid, longstem, long-stem, magic bullet, magic-bullet, makita tools, makita-tools, milwaukee, monster cable, monster-cable, mustee, nest, nutri____, nutribullet, oxo, party bullet, shark, simplehuman, sony bravia, urban decay, urban-decay, waterpik, weber grill, weber-grill, youthology, teeter 

下面是过滤基于TXT列表

// This grabs the data from the txt file and splits each word by commas 
$.get('word-list.txt', function(data) { 
    pbfFilterWords = data.split(', '); 
    pbfFilterWords.forEach(function(word){ 
    pbfWordList = word; 
}); 

// This defines a global variable so the filter button has a list of words to filter with 
var pbfWordList = pbfFilterWords; 

//This will allow the list of words to filter with regex 
var pbfRegEx = pbfFilterWords; 

var filtered = (function(){ 
    var filtered = [], i = pbfRegEx.length; 
    while (i--) { 
     if (/w*[\s|\w|\b+]*\w*/.test(pbfRegEx[i])) { 
      filtered.push(pbfRegEx[i]); 
     } 
    } 
    return filtered; 
})(); 
console.log(filtered.join()); 

// Function for filter button 
$('.pbf-link-container[contenteditable]').html; 
$('#pbf-filter').click(function(){ 
     var $pbfOutput = $('.pbf-link-container[contenteditable]').html(); 
     // Array of words for filter 
     var pbfFilterWords = pbfWordList; 
      // Output to new DIV and remove specified keywords from pbfFilterWords 
      $('.pbf-link-output').html($pbfOutput); 
      // To make pbfFilterWords not case sensitive 
      $.expr[":"].contains = $.expr.createPseudo(function(arg) { 
       return function(elem) { 
       return $(elem).html().toUpperCase().indexOf(arg.toUpperCase()) >= 0; 
        }; 
      }); 
      // Function to output the filtered words 
      $.each(pbfFilterWords , function(i , filtered){ 
      $('.pbf-link-output > div:contains("'+filtered+'")').remove(); 
     }); 
    }); 
}); 

这里所输入的话我的JS文件是我的html:

<div id="pbf-container"> 
    ..... 
     <div class="pbf-link-container" contenteditable="true"> 
     <div><br/></div> 
     </div> 
      <div class="pbf-button-control"> 
       <button id="pbf-filter"> Filter </button> 
      </div> 
        <div class="pbf-filter-header"> 
         <h3> Filtered Links </h3> 
        </div> 
       <div class="pbf-link-output"> 
       </div> 
</div> 
+0

你想过滤什么? –

回答

1

您的伪选择器实现需要html版本的div标签内部内容,所以它c反转&&AMP;,并且您没有匹配。

你可以看到自己:

$.expr[":"].contains = $.expr.createPseudo(function(arg) { 
    return function(elem) { 
    // console.log value of current element here 
    console.log($(elem).html().toUpperCase()); // logs string with &AMP; 
    return $(elem).html().toUpperCase().indexOf(arg.toUpperCase()) >= 0; 
    }; 
}); 

使用$(elem).text()获得的股利内内容的文字版本。

+0

啊,我明白了,谢谢你指出了。如果我想将自己的内容改为

,你认为它也能解决这个问题吗? – Publifiedlabs

+0

我不这么认为,因为html()仍然会将特殊符号转换为它们的html代码表示。我认为text()在这里是一个解决方案。 –

+0

明白了,谢谢你的帮助。我将(elem)切换到.text(),它现在正在工作:)。谢谢。 – Publifiedlabs

相关问题