How does one compare two strings in JavaScript without breaking if used in other cultures (such as Turkey and its notorious problems with the letter I)? Does JavaScript have any way of doing a culture-independent case-insensitive string comparison?
Yes, by simply using the standard .toLowerCase
method which is not affected by locale settings. a.toLowerCase() == b.toLowerCase()
is perfectly fine. The spec even mandates:
The result must be derived according to the case mappings in the Unicode character database
This definitely is consistent across all systems regardless of their settings.
For respecting the current locale, you would use .toLocaleLowerCase
explicitly. MDN states:
In most cases, this will produce the same result as toLowerCase(), but for some locales, such as Turkish, whose case mappings do not follow the default case mappings in Unicode, there may be a different result.