escapeRegex causing no results when searching ip addresses in column
escapeRegex causing no results when searching ip addresses in column
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
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.
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
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 thesearch
docs. Try this:See this example:
http://live.datatables.net/hepitegi/1/edit
Kevin
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."
@colin The plugin is actually just for sorting. It doesn't have an affect on searching at all.