Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToLowerCase in Chrome vs Firefox

Tags:

javascript

The following gives 0 in Firefox. However it gives -1 in chrome.

var index = "İSTANBUL".toLowerCase().indexOf("is");
console.log(index);

https://jsfiddle.net/81f0yr8w/1/

Chrome puts an extra character when lower casing İ (latin capital letter i with a dot above "\u0130")

"İ".toLocaleLowerCase().length
>2

Is it a normal behaviour?

like image 795
cgon Avatar asked Mar 08 '16 15:03

cgon


People also ask

What is the difference between toLowerCase and toLocaleLowerCase?

The toLocaleLowerCase() method does not change the original string. The toLocaleLowerCase() returns the same result as toLowerCase() , except for locales that conflict with the regular Unicode case mappings (such as Turkish).

What is toLowerCase in JavaScript?

The toLowerCase() method converts a string to lowercase letters.

How do I convert a string to lowercase in TypeScript?

The toLowerCase() is an inbuilt function in TypeScript which is used to convert the characters within a string to lowercase.


1 Answers

You can encode the string first then compare it. This will yield the same result in Firefox and Chrome.

// 0 Chrome // 0 Firefox
encodeURIComponent("İSTANBUL".toLowerCase()).indexOf(encodeURIComponent("İS".toLowerCase())) 

The fact that Firefox and Chrome handle it differently is strange. But strange is defined by w3c spec here: https://www.w3.org/TR/charmod-norm/#matchingAlgorithm fwiw, you have to lowercase the string first then encode it. They won't match unless they're the same case first.

Found the Firefox Bug and marked duplicate of [812837]. (https://bugzilla.mozilla.org/show_bug.cgi?id=812837) It was reported in 2011 and it's still open. So I guess it's not a priority.

like image 187
Shanimal Avatar answered Sep 24 '22 02:09

Shanimal