Home Ask Login Register

Developers Planet

Your answer is one click away!

johnny_s February 2016

Datatables clear search input and regex

I am using datatables with some regex filtering. All is working as expected apart from one minor thing.

When I search for a term, the table is filtered successfully. However whenever I click anywhere away from the search box, there is a \b prepended to the search term.

For example, search for Lon, the click off the search input. You will see that this then changes to \bLon.

I'm not sure why this is hapenning. Perhaps somebody could have a look at my code so far and spot something I am (obviously) missing!

Jsfiddle here.

My code so far;

    // DataTable
    var table = $('#example').DataTable();

    $('.dataTables_filter input').unbind().bind('keyup change', function(e) {
      var searchTerm = this.value.toLowerCase(),
        regex = '\\b' + searchTerm + '';
      table.rows().search(regex, true, false).draw();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.js"></script>
<link href="https://cdn.datatables.net/1.10.0/css/jquery.dataTables.css" rel="stylesheet"/>

<table id="example" class="display" cellspacing="0" width="100%">
      <th>Start date</th>

      <td>Tiger Nixon</td>
      <td>System Architect</td>        


Manoj Kulkarni February 2016

You are using regex = '\\b' + searchTerm + ''; ( May be for removing blank spaces) Please remove it just use regex = searchTerm; we can able to search records.

Post Status

Asked in February 2016
Viewed 1,441 times
Voted 12
Answered 1 times


Leave an answer

Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved