javascript - DataTables regex search not working as expected for exact match - Stack Overflow

I want to search a column of my DataTable for an exact value and return only the row that contains that

I want to search a column of my DataTable for an exact value and return only the row that contains that value. I've read that the way to do this is to do a regex search for the specific value, however when I attempt this an exact regex search returns nothing.

For example in the following table I want to return only the row that contains an id=0

<table id="searchTable" class="formTable display dataTable" cellspacing="0" style="width: 100%;">
    <thead>
        <tr>
            <th>id</th>
            <th>Position</th>
            <th>Office</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>0</td>
            <td>Accountant</td>
            <td>Tokyo</td>
        </tr>
        <tr>
            <td>1</td>
            <td>Chief Executive Officer (CEO)</td>
            <td>London</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Junior Technical Author</td>
            <td>San Francisco</td>
        </tr>
        <tr>
            <td>10</td>
            <td>Software Engineer</td>
            <td>London</td>
        </tr>
        <tr>
            <td>20</td>
            <td>Software Engineer</td>
            <td>San Francisco</td>
        </tr>
    </tbody>
</table>

When the document is loaded I attempt to use the search api as described here.

var table = null;
$(document).ready(function(){
    table = $('#searchTable').DataTable( {
        "sPaginationType": "full_numbers",
        "iDisplayLength": 5
    } );

table.columns(0).search('/0/',true,false).draw();

} );

JS fiddle showing the regex failing

I want to search a column of my DataTable for an exact value and return only the row that contains that value. I've read that the way to do this is to do a regex search for the specific value, however when I attempt this an exact regex search returns nothing.

For example in the following table I want to return only the row that contains an id=0

<table id="searchTable" class="formTable display dataTable" cellspacing="0" style="width: 100%;">
    <thead>
        <tr>
            <th>id</th>
            <th>Position</th>
            <th>Office</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>0</td>
            <td>Accountant</td>
            <td>Tokyo</td>
        </tr>
        <tr>
            <td>1</td>
            <td>Chief Executive Officer (CEO)</td>
            <td>London</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Junior Technical Author</td>
            <td>San Francisco</td>
        </tr>
        <tr>
            <td>10</td>
            <td>Software Engineer</td>
            <td>London</td>
        </tr>
        <tr>
            <td>20</td>
            <td>Software Engineer</td>
            <td>San Francisco</td>
        </tr>
    </tbody>
</table>

When the document is loaded I attempt to use the search api as described here.

var table = null;
$(document).ready(function(){
    table = $('#searchTable').DataTable( {
        "sPaginationType": "full_numbers",
        "iDisplayLength": 5
    } );

table.columns(0).search('/0/',true,false).draw();

} );

JS fiddle showing the regex failing

Share Improve this question asked Feb 23, 2016 at 10:36 GrantD71GrantD71 1,8754 gold badges20 silver badges27 bronze badges 1
  • 1 Maybe you just need to remove 's? table.columns(0).search(/0/,true,false).draw();. Note ^0$ will match for a 0 that is equal to a whole string. /0/ will search for a 0 inside longer strings. – Wiktor Stribiżew Commented Feb 23, 2016 at 10:46
Add a ment  | 

2 Answers 2

Reset to default 3

The trick was to put the start character symbol '^' before the value being searched on and end character symbol '$' after the value. Without giving these two symbols the regex will always return nothing.

Fixed portion:

var table = null;
$(document).ready(function(){
    table = $('#searchTable').DataTable( {
        "sPaginationType": "full_numbers",
        "iDisplayLength": 5
    } );

table.columns(0).search('^0$',true,false).draw();

} );

In case somebody else face this problem. In my case the problem was related to searching for string characters that have special meaning in a regular expression (eg. "AC (JLHA2) - GB/T1179-2008" will give nothing even if the data exists in the table).

I was able to fix this by using $.fn.dataTable.util.escapeRegex() to escape all special characters.

Here is the fix:

var table = null;
    $(document).ready(function(){
        table = $('#searchTable').DataTable( {
            "sPaginationType": "full_numbers",
            "iDisplayLength": 5
        } );

        // Escape the expression so we can perform a regex match    
        var val = $.fn.dataTable.util.escapeRegex('AC (JLHA2) - GB/T1179-2008');
        table.columns(0).search(val ? '^' + val + '$' ; '', true, false).draw();        
    } );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745481768a4629588.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信