Global regex filtering

Global regex filtering

mimicmimic Posts: 43Questions: 0Answers: 0
edited November 2009 in General
Is there a way to enable regex in DataTables global filter?

(I am sorry if I have missed something obvious.)

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi mimic,

    If you are using the API you can just pass 'null' as the second parameter to fnFilter ( http://datatables.net/api#fnFilter ). Or if you want it on the default global filter you can use oTable.fnSettings().oPreviousSearch.bEscapeRegex = false;

    Regards,
    Allan
  • mimicmimic Posts: 43Questions: 0Answers: 0
    Thanks, it works. But it is strange that it cannot be set normally. So I have now:

    [code]
    $('#table').dataTable(... settings ...).fnSettings().oPreviousSearch.bEscapeRegex = false;
    [/code]
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi mimic,

    Actually it is possible through the initialisation object. You can use oSearch which is an object which takes two parameters bEscapeRegex and sSearch.

    Regards,
    Allan
  • mimicmimic Posts: 43Questions: 0Answers: 0
    Great, this is much much better.

    But I have found a problem. I would like to use only global filter and I would like to use regex in it. But in _fnFilter where you generate the regular expression to use you break it apart and then combine it in a way the most useful regex does not work, "^prefix" for example, as you make out of it "^(?=.*?^prefix).*$", what does not really work as expected. (In comment about you also mention \b in regex but you do not use it later in code to generate regex.)
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi mimic,

    Yes indeed, DataTables tries to be "clever" to make it's default filter as versatile as possible for casual implementation. But as you are seeing, this can interfere with custom complex regular expressions. In order to do this kind of filtering you'll need to make use of one of the APIs that DataTables presents - a custom global filter: http://datatables.net/development/filtering . It might not be quite as easy are the standard global filter, but it is massively more flexible.

    Hope the helps,
    Allan
This discussion has been closed.