I have a javascript case conversion problem which I cannot solve due to non-English letters. My main concern is the Turkish alphabet.
What I need to do is this:
- hello world => Hello World
- HELLO WORLD => Hello World
- hELLO wOrLd => Hello World
Here is what I've accomplished so far:
String.prototype.turkishToUpper = function(){
var stringlow = this;
var letterslow = { 'i': 'İ', 'ş': 'Ş', 'ğ': 'Ğ', 'ü': 'Ü', 'ö': 'Ö', 'ç': 'Ç', 'ı': 'I' };
stringlow = stringlow.replace(/(([iışğüçö]))/g, function(letterlow){ return letterslow[letterlow]; })
return stringlow.toUpperCase();
}
String.prototype.turkishToLower = function(){
var stringup = this;
var lettersup = { 'İ': 'i', 'I': 'ı', 'Ş': 'ş', 'Ğ': 'ğ', 'Ü': 'ü', 'Ö': 'ö', 'Ç': 'ç' };
stringup = stringup.replace(/(([İIŞĞÜÇÖ]))/g, function(letterup){ return lettersup[letterup]; })
return stringup.toLowerCase();
}
String.prototype.toProperCase = function () {
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).turkishToUpper() + txt.substr(1).turkishToLower();});
};
But this does not give me the correct results and I am suspecting the regex replace not being usable on unicode, but ascii.
When I test with Turkish characters, I get wrong results.
- şeker becomes şEker instead of Şeker
- çoban ırmak becomes çOban ıRmak intead of Çoban Irmak
Also, if this can ever get resolved, I need an icing on the cake to separate words not only by spaces, but also by some other stop characters such as : - = / etc so that
- hello-world becomes Hello-World
- hello:world becomes Hello:World
I've read through many similar questions here on SO, but no luck so far.
Thanks
Note: I think this is called Title Case but some have argued that it is Pascal Case. To be frank, I am interested in resolving the unicode issue (which I believe is the root cause) rather than semantics, so please forgive me if I've used wrong terminology :)
Standalone function:
Or for extending of String.prototype:
Update:
Next code uses custom functionality for converting locale specific chars (tested partially). Code adds functions into
String.prototype
:toLocaleProperCase2
,toLocaleLowerCase2
andtoLocaleUpperCase2
.