Jeff Shain Jeff Shain - 3 months ago 35
jQuery Question

Datatables regex search for exact string (not the substring)

Let's say i have a table with three rows. In one column, i have these vaules


  1. coach

  2. Assistant Coach

  3. Coach assistant coach

  4. Coach



If i do a search now for "coach", all rows return. I don't want that. What I need is two sets of code to do the following operations:

1) to be able to type the word "coach" and only have it return rows 1 and 3.
2) to be able to type the word "coach" and only have it return row 1 (case sensitive)

In other words, I don't want any records returned that have the substring coach. Just coach. I haven't been able to figure this out as there is a space between assistant and coach and the regex i have recognizes the free standing word and returns it.

Let me know if this is possible.

My current code is:

var stringFilter = $(this).val();
var myregex = ("\\b" + stringFilter + "\\b");
$("##add_grid").DataTable().column().columns(7).search(myregex,true,false).draw();

Answer

If you want search by exact string you can use column().search() try this code:

dt.column(0).search("^" + "coach" + "$", true, true, false).draw();

Result: https://jsfiddle.net/cmedina/7kfmyw6x/87/

Comments