datatable regular expression searching

datatable regular expression searching

Joe LighthallJoe Lighthall Posts: 2Questions: 1Answers: 0

I cannot figure out how to get an exact match regular expression search working. I've even tried what is suggested on the search help page.

Here is a link to what I'm trying to do.
http://live.datatables.net/jatudaci/2/edit?html,css,js,console,output

Hopefully i got the link right. If not the javascript is below.

Thanks,
Joe

$(document).ready( function () {
table = $('#example').DataTable();

// shows nothing - i expect it to show only rows starting with Director
// this is ultimately what i want
table.search('^Director$', true, false).draw();

// shows nothing - given this is what is provided on the search example
// i would expect this to also show rows starting with Director
table.search('^\sDirector\s*$', true, false).draw();

// shows all records with director (inlcuding my bogusDirector)
table.search('Director', true, false).draw();

// shows all records with director as a boundary
// the regex works for boundary
table.search('\bDirector\b', true, false).draw();

// how do i get the first search to work???
} );

Answers

  • Joe LighthallJoe Lighthall Posts: 2Questions: 1Answers: 0

    Well I think I've found a solution. All I'm really trying to is see if an exact match is found.

    I ended up using filter. Something like this. This is not the actual look of what i want but filter is how i find the exact match.

    var cnt = table.column(1).data().filter( function ( value, index ) {
    //console.log(value);
    var test = (value == 'Director');
    if (test) {
    console.log(value + ' test:' + test);
    }

    return test;
    } ).length;
    

    console.log('cnt:' + cnt)

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin

    table.search('^Director$', true, false).draw();

    This would for with column().search(), but it doesn't work with the global search() method because of how DataTables internally implements the search. Basically, instead of searching each cell in the row individually (which is slow) it will store a cache of a double space separated string for the row's data and run the regex on that. Using a boundary is the best way to go with the global search.

    I realise this is a bit of a pain, and you aren't the first to bring it up. Its a trade off between performance and utility. I plan to revamp the search in future and will keep this in mind.

    Regards,
    Allan

This discussion has been closed.