Last active
April 22, 2024 13:11
-
-
Save enginkartal/84a215589e6739de6f0cbeb9fd088e4f to your computer and use it in GitHub Desktop.
Javascript Turkish character to english characters change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.turkishtoEnglish = function () { | |
return this.replace('Ğ','g') | |
.replace('Ü','u') | |
.replace('Ş','s') | |
.replace('I','i') | |
.replace('İ','i') | |
.replace('Ö','o') | |
.replace('Ç','c') | |
.replace('ğ','g') | |
.replace('ü','u') | |
.replace('ş','s') | |
.replace('ı','i') | |
.replace('ö','o') | |
.replace('ç','c'); | |
}; | |
// Example | |
// let text = 'Ne güzel bir gökkuşağı'; | |
// newText = text.turkishtoEnglish(); | |
// console.log(newText) /Ne guzel bir gokkusagi | |
Herkese merhaba, biraz daha guncel bir cozum:
With ES2015/ES6 String.prototype.normalize(),
const turkish_letters_to_convert = 'Ç,Ğ,İ,Ö,Ş,Ü,ç,ğ,ı,i,ö,ş,ü';
console.log(turkish_letters_to_convert.normalize('NFD').replace(/\p{Diacritic}/gu, ''));
// Expected output: "C,G,I,O,S,U,c,g,ı,i,o,s,u"
The example uses the combination of "Canonical Decomposition (NFD)" with Unicode property escapes.
See the source: https://stackoverflow.com/a/37511463
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Multer'a Upload ettiğim Türkçe karakterli dosya isimlerini Save etmeden önce temizlemek istemiştim. Benim için aşağıdaki çözüm çalıştı.
const UTF8tarzanca = (str) => {
const sonuc = str
.replace(/\xC4\xB1/g, 'i')
.replace(/\xC3\xA7/g, 'c')
.replace(/\xC5\x9F/g, 's')
.replace(/\xC3\xB6/g, 'o')
.replace(/\xC3\xBC/g, 'u')
.replace(/\xC4\x9F/g, 'g')
.replace(/\xC4\xB0/g, 'I')
.replace(/\xC3\x87/g, 'C')
.replace(/\xC5\x9E/g, 'S')
.replace(/\xC3\x96/g, 'O')
.replace(/\xC3\x9C/g, 'U')
.replace(/\xC4\x9E/g, 'G')
return sonuc
}