---
title: "Filter Elements by Pattern jQuery."
description: "The need was pretty simple : ’Find the elements that contain a particular word and display them and hide others’ Along with."
url: "https://h3manth.com/scribe/filter-elements-pattern-jquery/"
canonical_url: "https://h3manth.com/scribe/filter-elements-pattern-jquery/"
markdown_url: "https://h3manth.com/scribe/blog/filter-elements-pattern-jquery.md"
publishDate: "2013-02-23T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["python","javascript","web"]
word_count: 113
reading_time_minutes: 1
x_markdown_tokens: 320
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Filter Elements by Pattern jQuery.

> The need was pretty simple : ’Find the elements that contain a particular word and display them and hide others’ Along with.

The need was pretty simple : "Find the elements that contain a particular word and display them and hide others" Along with my friend Kambfhase we came with a simple hack to achieve this, well this in itself is very simple to be a jQuery plugin but can be a part of huger system.

**Code :**

```javascript
(function($) { 
  $.fn.filterByPattern = function(string){
    var regex = new RegExp(string, "ig");
    
    return this.hide().filter(function(){
      return regex.test($(this).text());
    }).show();
  }; 
}(jQuery));
```

**Usage:** 

```javascript
$("div").filterByPattern("python")
```

**Extra :** Used the same at [codingconfessional.com](http://www.codingconfessional.com/) to filter out the needed, for example:

```javascript
$("div.confession").filterByPattern("python")
```

will show all the python confessions ;)

**Update 0:** Why was contains() not used? [Issue 278](http://bugs.jquery.com/ticket/278)
