1

I am using jQuery Datatables plugin in my application. I am trying to search for a string that exactly matches the data in a column. I checked jQuery DataTables - Filter column by an exact match which is not working for my case. My search string is a regular expression with | symbols which might look like s1|s2|s12. When the search string contains only s1 at that time s1 record is shown , but when i search multiple values s1|s2 then no records are shown

jQuery("#myTable").DataTable()
                    .columns("#")
                    .search("^"+'s1|s2'+"$",true,false)
                    .draw();

Result: No result found .

when i tried below code then addtional records are shown up

$('#myTable').DataTable().search('s1|s2').draw();

Result: s1,s2,s11,s14,s22,s25 etc.

Any help would be much appreciated ..

Vikas Yadav
  • 3,094
  • 2
  • 20
  • 21
user202
  • 758
  • 10
  • 26

1 Answers1

2

You missed by two brackets.

jQuery("#myTable").DataTable()
                    .columns("#")
                    .search('^(s1|s2)$',true,false)
                    .draw();

And search('s1|s2') did not give desired results because by default regex is disabled in search method. So it is considering s1|s2 as a string which will match s, 1, 2, |, s1, 1| .. etc. You have to provide second parameter as true and third parameter as false to avoid conflict and cause unexpected results. See search() method for more information.

Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40