How to use regex for multi-value whole-word column search, e.g., A or B?

How to use regex for multi-value whole-word column search, e.g., A or B?

mjbellermjbeller Posts: 2Questions: 1Answers: 0
edited August 2015 in Free community support

I'm using https://datatables.net/examples/api/multi_filter.html

along with vbrouchet comment for using ";" to enter multiple search conditions:

.search (this.value.replace(/;/g, '|'), true, false)

That works nicely, I can enter "1;3" in the column search to find all values in the column with "1" or "3". I would like to search for individual integers/whole words (e.g., "1" should not match "12"). I have a regex that works, e.g., to search for 1 I can use \b1\b.

If I want to search for "1" or "3": \b1\b|\b3\b1

I've tried various options but can't replace the entered string with the right regex.

I've tried:

$( 'input', this.footer() ).on( 'keyup change', function () {
    // build search string using \b1\b
    var arr = this.value.split(';');
    var pattern = "\b" + arr.join("\b|\b") + "\b"
    that
        // .search( this.value.replace(/;/g, '|'), true, false )
        .search( this.value = pattern, true, false )
        .draw();
} );

I have a fiddle to play : http://jsfiddle.net/mjbeller/3zqswwzt/

I also posted on SO: http://stackoverflow.com/questions/32024776/how-to-use-regex-for-multi-value-or-column-search-using-datatables

Answers

  • mjbellermjbeller Posts: 2Questions: 1Answers: 0

    This works:

        table.columns().every( function () {
            var that = this;
            $( 'input', this.footer() ).on( 'keyup change', function () {
                var arr = this.value.split(';');
                var pattern = ("\\b" + arr.join('\\b|\\b') + '\\b');
                that
                    .search( pattern, true, false )
                    .draw();
            } );
        } );
    
This discussion has been closed.