Is it possible to filter/search by not matching a specific character in regex?

Is it possible to filter/search by not matching a specific character in regex?

tamara_snydertamara_snyder Posts: 2Questions: 1Answers: 0
edited July 2015 in Free community support

We are using DT 1.10. I have seen a few others ask this question but with no replies which leads me to suspect it cannot be done, but perhaps there is a way. Is it possible to search a column and return all records which do not contain a plus sign "+"? This works outside of DT: ^((?!\+).)*$ however it does not work with .search(). We use column header filters extensively with great success with positive regex matches, however this negative matching filter we cannot get to work in Datatables. Thanks in advance for any help.

This question has an accepted answers - jump to answer

Answers

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    edited July 2015

    In general there is no problem with regex not in datatables filtering, in my yadcf plugin there is an option for such "not" filtering, you can see it action in this showcase page notice the 4'th column (values) there is a checkbox (called "exclude") that allow you to do the "not" filtering.

    Anyway you should escape your ((?!+).) expression ,

    something like this:

    function escapeRegExp(string) {
        return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    }
    
        var someStr = escapeRegExp('((?!+).)');
        XYZ.search('^' + someStr + '*$');
    
  • allanallan Posts: 61,782Questions: 1Answers: 10,112 Site admin
    Answer ✓

    Anyway you should escape your ((?!+).) expression

    In part that was an error in the forum. For some reason it is stripping out escapes unless they are in Markdown code tick marks... I've added them now.

    However, that you say is absolutely true - DataTables creates the regex using new RegExp(...) - so it does need another escape: '^((?!\\+).)*$'.

    Example.

    I think for 1.11 passing a regex into search() might be quite a nice feature.

    Allan

  • tamara_snydertamara_snyder Posts: 2Questions: 1Answers: 0

    Thank you very much Daniel and Allan, yes in fact the missing additional backslash was the problem, this works great now.

    Thanks again, appreciate it.

    Tammy

This discussion has been closed.