$.fn.dataTable.util.escapeRegex() with array and .split()

$.fn.dataTable.util.escapeRegex() with array and .split()

JaverbuchJaverbuch Posts: 2Questions: 1Answers: 0

I am trying to use a multi select drop-down box to filter data in my table. Some of the values contain special characters that need to be escaped so I can use them in my regex .search() method. The issue is that I first need to join the array into a string to pass it to $.fn.dataTable.util.escapeRegex(). This ends up causing the "|" delimiter (OR operator for REGEX) to be escaped and not read properly in the search.

My question is: Is there any way to use the $.fn.dataTable.util.escapeRegex() for this situation without escaping the "|" delimiter? If not, I am assuming my best/only option is to loop through the selected elements and escape them one by one.

                var val = $.fn.dataTable.util.escapeRegex(
                    $(this).val().join("|")
                     // This results in val1 \| val2 \| val3
                     // I need val1 | val2 | val3
                );

                column
                    .search(val ? '^'+val+'$' : '', true, false)
                    .draw();

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,922
    Answer ✓

    According to the $.fn.dataTable.util.escapeRegex() docs it expects a string parameter.

    This results in val1 | val2 | val3

    After the API you could use regex to remove the \. Haven't tested it but maybe something like this val = val.replace(/\\\|/g, '|');.

    Kevin

  • JaverbuchJaverbuch Posts: 2Questions: 1Answers: 0

    Spot on, I am banging my head against the wall because I forgot that I can use regular expressions with replace. Thank you for the assistance!

This discussion has been closed.