2017-08-26 40 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

相關問題