Solution 1 :

Loop over the search terms and check them each individually. No need for regex.

I would also question whether you really need jQuery; this code would have been just as easy to write without it and it’s then more flexible.

$(document).ready(function() {
  (function($) {
    $('#filter').keyup(function() {
      var terms = this.value.split(' ');
      $('.searchable tr').hide();
      $('.searchable tr').filter(function() {
        for (var term of terms) {
          if (this.textContent.indexOf(term) < 0) return false;
        }
        return true;
      }).show();
    })
  }(jQuery));
});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<input id="filter" type="search" placeholder="Search">
<table>
  <tbody class="searchable">
    <tr>
      <td>love and dance </td>
      <td> something else</td>
    </tr>
    <tr>
      <td>play and eat </td>
      <td></td>
    </tr>
    <tr>
      <td>love and roll </td>
      <td></td>
    </tr>
  </tbody>
</table>

Solution 2 :

You can use the javascript includes() function to check if your string is a substring of the text.

$(document).ready(function() {
  (function($) {
    $('#filter').keyup(function() {
      var filterText = $(this).val();
      $('.searchable tr').hide();
      $('.searchable tr').filter(function() {
        return $(this).text().includes(filterText)
      }).show();
    })
  }(jQuery));
});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<input id="filter" type="search" placeholder="Search"></form>
<table>
  <tbody class="searchable">
    <tr>
      <td>love and dance </td>
      <td> something else</td>
    </tr>
    <tr>
      <td>play and eat </td>
      <td></td>
    </tr>
    <tr>
      <td>love and roll </td>
      <td></td>
    </tr>
  </tbody>
</table>

Problem :

I would like to have a table which has words included in td. I am wondering if there is any way that search result shown irrespective of the way user searched. for example td contains ” love and dance and sing ” but the code that I have show result only when user types “love and dance and sing ” . If user type “dance and love” it doesn’t show.I want to show result irrespective of arrangement of words,
ie , if user types “sing and dance” or dance and love” result should show up.

Any help would be really appreciated. Thanks in Advance

Here is my code

$(document).ready(function() {

  (function($) {

    $('#filter').keyup(function() {

      var rex = new RegExp($(this).val(), 'i');
      $('.searchable tr').hide();
      $('.searchable tr').filter(function() {
        return rex.test($(this).text());
      }).show();

    })

  }(jQuery));

});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<input id="filter" type="search" placeholder="Search"></form>
<table>
  <tbody class="searchable">
    <tr>
      <td>love and dance </td>
      <td> something else</td>
    </tr>
    <tr>
      <td>play and eat </td>
      <td></td>
    </tr>
    <tr>
      <td>love and roll </td>
      <td></td>
    </tr>
  </tbody>
</table>

Comments

Comment posted by David Becckam

Thanks you Sir ,Exactly what I wanted

By