Reputation: 165
I have a filter search for my data. When I type turkish characters it doesn't work. How can I modify this code?
var $rows = $('#tableExport tr');
$('#search').keyup(function() {
var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
reg = RegExp(val, 'i'),
text;
$rows.show().filter(function() {
text = $(this).text().replace(/\s+/g, ' ');
return !reg.test(text);
}).hide();
});
These are the characters ç ,ı ,ğ, ö , ş , ü , Ç, İ , Ğ, Ö, Ş, Ü
Upvotes: 1
Views: 1653
Reputation: 140
if you guys are using react or angular must try this one.
const reg = new RegExp(q.toUpperCase().replaceAll(/[İi]/ig, '[iİ]'), 'i')
q could be your object or string that comes from client.
Upvotes: 1
Reputation: 627082
Here, there are two problems: 1) \b
is not Unicode aware and 2) i
when used in a case insensitive pattern will only match a dotless I
, not the dotted counterpart.
I suggest two scenarions: 1) whitespace boundaries, or 2) ASCII word chars + Turkish letters based boundaries.
The patterns are very similar. Here is an example of a whitespace boundary based code:
var val = '^(?=.*(?:^|\\s)(?:' + $.trim($(this).val()).split(/\s+/).join('|') + ')(?!\\S))',
reg = RegExp(val.replace(/[İi]/ig, '[iİ]'), 'i'),
Here, the pattern looks like /^(?=.*(?:^|\s)(?:[iİ]ç|Anadolu)(?!\S))/i
. Note that .replace(/[İi]/ig, '[iİ]')
will allow matching dotted İ
with an i
.
The other approach means we need to replace \b
with a lookaround that negated an ASCII word char (\w
) with a Turkish letter:
var val = '^(?=.*(?:^|[^\\wçığöşüÇİĞÖŞÜ])(?:' +
$.trim($(this).val()).split(/\s+/).map(function(x) {
return x.replace(/[İi]/ig, '[iİ]');
}).join('|') + ')(?![\\wçığöşüÇİĞÖŞÜ]))',
reg = RegExp(val, 'i'),
Here, the pattern will look like /^(?=.*(?:^|[^\wçığöşüÇİĞÖŞÜ])(?:[iİ]ç|Anadolu)(?![\wçığöşüÇİĞÖŞÜ]))/i
and will safely match a term in between non-word or non-Turkish letters.
Upvotes: 2