Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Allow user to type regular expression into column search bar, datatables

I would like to be able to type a regular expression, for example ^.{2}06.{4,}$ into my individual column search bars. For reference I was able to do this when I was using the client side version of DataTables, but since switching over to server side to handle my larger table I haven't been able to replicate this.

Here is a link to the example I have built off of https://www.datatables.net/examples/api/multi_filter.html

And here is my current code for the initialization of the table

var table = $('#results').dataTable({
        processing: true,
        serverSide: true,
        ajax: 'getTable.php',
        orderCellsTop: true,
        fixedHeader: true,
        "search" : { //this is what I have changed from the DataTables example//*
            "regex": true                                                     //*
        },                                                                    //*
        createdRow: function ( row ) {                                        //*
            $(row).addClass("hover");                                         //*
        },                                                                    //*
        //***********************************************************************
        initComplete: function () {
            this.api()
                .columns()
                .every(function () {
                    var that = this;
                    
                    $('input', this.footer()).on('keyup change clear', function () {
                        if (that.search() != this.value){
                            that.search( this.value ).draw();
                            
                            //I have also tried setting the optional regex value of the 
                            //search function to true with no success
                            //that.search( this.value , true ).draw();
                        }
                    });
                });
        }

    });

If anyone could help me with this it would be greatly appreciated

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Look at the Sent Parameters section of the Server-Side Processing page in the official manual.

You will see two things of relevance:

search[regex] - true if the global filter should be treated as a regular expression for advanced searching, false otherwise.

And:

columns[i][search][regex] - Flag to indicate if the search term for this column should be treated as regular expression (true) or not (false).

So, your server-side route handler needs to parse the sent parameters in the response, to extract these booleans for each of your column filters. These values will determine if the column search term should be treated as a regex or not.

You also need to use the DataTables searchCols option to indicate which of your columns' search terms should be treated as regexes - you cannot only use the search: { "regex": true } option because that is only for the global search field.

An example:

searchCols: [
  { regex: false },
  { regex: true },
  { regex: true },
  { regex: true },
  { regex: true },
  { regex: true }
]

The above example assumes a table with 6 columns, and all of their search terms - except for the first column - are to be treated as regexes.

The data can be found in the request body at columns[i][search][regex], as noted above.


There is a warning in the Data Tables documentation which is worth repeating:

As with global search, normally server-side processing scripts will not perform regular expression searching for performance reasons on large data sets, but it is technically possible and at the discretion of your script.


Final Note:

Client-side (DataTables) search logic is ignored when you use serverSide: true. That includes the DataTables search() API call included in your question:

that.search( this.value ).draw();

That is one of the fundamental points about using server-side processing for your DataTable: All logic for paging, sorting and filtering is handled on the server - and none of it is provided by DataTables. You have to provide all that logic yourself.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!