Jeff Shain Jeff Shain - 1 year ago 181
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");

Answer Source

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

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download