0

I'm writing a simple filter for a table of results but I have a problem when one of the fields being filtered contains an apostrophe.

How I'm currently handling it is the table rows have data attributes of the filterable fields and for every filter I'm trying to hide rows that don't match the filter options.

I've tried escaping the apostrophe (replace(/'/g, "\'") +'"]')) and using JSON.stringify but each time it says "unrecognised expression".

 $('#filtered_table tr').not('[data-channel="Owner\'sDirect"]');
 $('#filtered_table tr').not('[data-channel=\"Owner\'sDirect\"]')

Error: Syntax error, unrecognized expression: data-channel="Owner'sDirect"]

$('#filtered_table tr').not('[data-channel=\"Owner\\\'sDirect\"]');
$('#filtered_table tr').not('[data-channel="Owner\\\'sDirect"]');

Error: Syntax error, unrecognized expression: data-channel="Owner\'sDirect"]

The real string actually has a space in between the words but including that makes the error Error: Syntax error, unrecognized expression: Direct"] so I removed the space just to rule that out as being relevant.

I have tried the solution in this 14 year old question that has marked mine as a duplicate jQuery selector value escaping and it did not work. Using

'[data-channel="'+value.replace(/([ #;?%&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1')+'"]'

results in Uncaught Error: Syntax error, unrecognized expression: data-channel="Owner'sDirect"]

I'm also open to other methods of filtering the table by user-defined strings and obviously I could just manipulate the data to remove apostrophes but I'd prefer not to if I don't have to.

0 Answers0