Steven Ackley
Steven Ackley

Reputation: 591

Strip White Characters Before Searching

I have a jQuery datatable that is using a regex search. Now lets say for example I have the following elements in the table.

Apple

Orange

Pear

When I search using regex, I want Apple and Orange so I would use "app|org". If I use "app | org" I only get back Orange. How would I go about stripping out the white space so I can get the same result as "app|org" when there is white space when searching?

var table = $("#YTDTable").dataTable({
        "sDom": 'T<"clear">lfrtip',

        "tableTools": {
            "sSwfPath": "//cdn.datatables.net/tabletools/2.2.3/swf/copy_csv_xls_pdf.swf",
            "aButtons": ["copy", "print", "xls", "pdf"]
        },
        "oSearch": { "bSmart": false, "bRegex": true },
        "responsive": true

Upvotes: 1

Views: 594

Answers (2)

Tushar
Tushar

Reputation: 87203

You can use Regular Expression to search and replace all the space characters. Use \s with g flag in replace() to remove all the space characters from a string.

var spaceReplacedString = 'a | b | c'.replace(/\s/g, '');

alert(spaceReplacedString);

Here \s searches for all the space characters like spaces, tabs, etc.

Upvotes: 2

Steven Ackley
Steven Ackley

Reputation: 591

I found a solution, it is combining Tushar's answer with the following

$('.dataTables_filter input').on('keyup click', function () {
        var val = $(".dataTables_filter input").val().replace(/\s/g, '');
        filterGlobal(val);
    });

    function filterGlobal(val) {
        table.api().search(val, true, false).draw();
    }

Upvotes: 0

Related Questions