title: Whole-word matching with jQuery and contains()
body: <p>I'm writing a Greasemonkey script to selectively hide elements containing nasty stuff (a personal web sanitizer, if you will).</p>
  
  <p>Here's what I've got so far:</p>
  
  <pre><code>//custom contains function which is case-insensitive
  $.extend($.expr[":"], {
    "containsNC": function(elem, i, match, array) {
      return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) &gt;= 0;
    }
  });
  
  //build array of words to filter
  var nope = "long list of horrible words".toLowerCase().split(' ');
  
  //start with an empty jQuery object
  var nopeEles = $();
  
  //add elements to filter to it
  for (var i = 0; i &lt; nope.length; i++) {
    nopeEles = nopeEles.add( $("a:containsNC('" + nope[i] + "')") );
    nopeEles = nopeEles.add( $("p:containsNC('" + nope[i] + "')") );
  }
  
  //hide all applicable elements
  nopeEles.css("background-color", "white");
  nopeEles.css("color", "white");
  </code></pre>
  
  <p>It works decently, but it does partial word matching, which makes short words not work. I want to filter elements containing words like "die" and "gun", without filtering those with words like "candied" or "gung-ho".</p>
  
  <p>To be clear, I'm after whole-word, not exact-text. I want "gun" in the list to match not just "gun" but also "he fired a gun" and "a gun was fired". And not "gunney sergeant".</p>
  
  <p>Every other answer I've seen on this topic recommends jQuery's filter(). I think I don't understand it well enough. I tried using this line in the loop, but nothing:</p>
  
  <pre><code>nopeEles = nopeEles.add( $("a").filter(function() { return $(this).text() === nope[i]; }) );
  </code></pre>
  
  <p>The other angle I thought to look at was fiddling with containsNC so it looks for the word, but with whitespace or end-of-string on either side. I don't really get how containsNC works, though.</p>
  
  <p>Any pointers would be hugely appreciated!</p>