escapeRegex causing no results when searching ip addresses in column

escapeRegex causing no results when searching ip addresses in column

mihomesmihomes Posts: 165Questions: 23Answers: 0

I'm using the below for a custom search box. I am NOT using serverside, but rather loading data with ajax. I have a column that displays ip addresses such as xxx.xxx.xxx.xxx. I realized searching for an ip address was not returning those rows and after some investigation I 'think' the issue is escapeRegex.

When using the below and searching for xxx.xxx.xxx.xxx the console will print out xxx\.xxx\.xxx\.xxx which then doesn't match the value in my column.

If I don't use escapeRegex it correctly displays the rows with the matching ip address. What is the solution here? The search value should be escaped, but then it is not properly matching column values in this particular case. This also has me wondering what other searches are not properly being returned in other tables where I'm not using serverside to handle the searching.

    selectSearch.keyup(function() {
        dt.search( $.fn.dataTable.util.escapeRegex(selectSearch.val()) ).draw();
        console.log( $.fn.dataTable.util.escapeRegex(selectSearch.val()) );

        //dt.search( selectSearch.val() ).draw();
        //console.log( selectSearch.val() );
    });

This question has an accepted answers - jump to answer

Answers

  • mihomesmihomes Posts: 165Questions: 23Answers: 0

    For the meantime I am defining my ip address column as shown below... which escapes the column's values for searching only so it matches the escaped search.

    {
        "data": "ip_address",
        "type": 'ip-address',
        "render": function ( data, type, row ) {
            switch (type) {
                //special formats else default to normal
                case 'display':
                    return '<span class="badge badge-info"> '+data+' </span>';
                case 'filter':
                    return $.fn.dataTable.util.escapeRegex(data);
                default:
                    return data;
            }
        }
    },
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    With that ip-address type defined it should 'just work'. Are you able to create a test case with the data/problem that demonstrates the problem, please?

    Colin

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    Answer ✓

    The intention of the $.fn.dataTable.util.escapeRegex() API is to work with regex searches. You are performing the default Smart Search as described in the search docs. Try this:

    dt.search( $.fn.dataTable.util.escapeRegex(selectSearch.val()), true, false ).draw();
    

    See this example:
    http://live.datatables.net/hepitegi/1/edit

    Kevin

  • mihomesmihomes Posts: 165Questions: 23Answers: 0

    Good catch! I guess I assumed the defaults were the way to go here since this is a 'regular' search box, but apparently not. The defaults are all true, but then it says :

    "Note that to perform a smart search, DataTables uses regular expressions, so if enable regular expressions using the second parameter to this method, you will likely want to disable smart searching as the two regular expressions might otherwise conflict and cause unexpected results."

  • mihomesmihomes Posts: 165Questions: 23Answers: 0

    @colin The plugin is actually just for sorting. It doesn't have an affect on searching at all.

This discussion has been closed.