0

I have this code :

if (term.length > 0) {
  var inputVal = $("#input").val();
  if (inputVal.indexOf("jason") !== -1) {
    results = $.ui.autocomplete.filter(table, term);
  }
}

This works. But when I changed "jason" to "jasön" or "jasün" or something else which is a Turkish character, it doesn't work. Is it because the encoding of the js file? I changed the encoding to Unicode (utf-8 with signature) - Codepage 65001 then Turkish ISO 28599 but it didn't work. Can you tell me what I should do? Thanks.

Preview
  • 35,317
  • 10
  • 92
  • 112
jason
  • 6,962
  • 36
  • 117
  • 198
  • Can you clarify where you are changing "jason" to "jasön"? Is it the input value or the regex string? Are you expecting "jasön" to match the regex /jason/i or /jasön/i? – purplecat Jun 02 '17 at 14:31
  • @purplecat What I am trying to do is when input includes jasön, this line should work :` results = $.ui.autocomplete.filter(table, term);` It can be regex or string, but string is better I guess. Thanks. – jason Jun 02 '17 at 14:34
  • Do you want it to work the same regardless of whether the input is "jason" or "jasön"? – purplecat Jun 02 '17 at 14:58

1 Answers1

0

It does recognize Turkish characters, but you are doing an equality check on said string. What you have to understand is that even if ö is only an o with an accent, it's still a different character, thus making your conditional falsy, even if you are not doing a triple equality check.

'ö' == 'o'  // false
'ö' === 'o' // false

What you should do instead is convert the input value into a string without accents, since it's apparently what you were expecting. And this question is exactly what you are looking for, and I would say this answer would be the best one if you have access to ES6 features since it's pretty clean and simple to use

function convert (str) {
  return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
}

if (term.length > 0) {
  var inputVal = convert($('#input').val())
  if (inputVal.indexOf('jason') !== -1) {
    results = $.ui.autocomplete.filter(table, term)
  }
}

Otherwise if ES6 is not an option, the top answer should be fine to use, just create a function util that you can reuse anywhere you need to.

Regarding the encoding of the file, you should use utf-8 if dealing with special chars that Turkish does have.

Preview
  • 35,317
  • 10
  • 92
  • 112