Skip to content

Instantly share code, notes, and snippets.

@enginkartal
Last active April 22, 2024 13:11
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save enginkartal/84a215589e6739de6f0cbeb9fd088e4f to your computer and use it in GitHub Desktop.
Save enginkartal/84a215589e6739de6f0cbeb9fd088e4f to your computer and use it in GitHub Desktop.
Javascript Turkish character to english characters change
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
@onka13
Copy link

onka13 commented May 24, 2019

If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced.

https://www.w3schools.com/jsref/jsref_replace.asp

.replace(/Ğ/gim, "g")
		.replace(/Ü/gim, "u")
		.replace(/Ş/gim, "s")
		.replace(/I/gim, "i")
		.replace(/İ/gim, "i")
		.replace(/Ö/gim, "o")
		.replace(/Ç/gim, "c")
		.replace(/ğ/gim, "g")
		.replace(/ü/gim, "u")
		.replace(/ş/gim, "s")
		.replace(/ı/gim, "i")
		.replace(/ö/gim, "o")
		.replace(/ç/gim, "c");

@ozcanzaferayan
Copy link

Eline sağlık kardeşim

@hidonet
Copy link

hidonet commented Oct 6, 2020

Test etmedim ama sanki bu kod sadece küçük harfe çeviriyor halbuki şöyle olmalıydı;

.replace(/Ğ/gim, "G") .replace(/Ü/gim, "U") .replace(/Ş/gim, "S") .replace(/İ/gim, "I") .replace(/Ö/gim, "O") .replace(/Ç/gim, "C") .replace(/ğ/gim, "g") .replace(/ü/gim, "u") .replace(/ş/gim, "s") .replace(/ı/gim, "i") .replace(/ö/gim, "o") .replace(/ç/gim, "c");

@misaghkarimi
Copy link

thank you it was useful for me

@Akif9748
Copy link

Akif9748 commented Nov 5, 2021

Replace sadece 1 kez çalışıyor. Çözüm:

var g = ""
    for (const harf of "çevrilcek ğelime") {
        g += harf.split("ı").join("i").split("İ").join("I").toLowerCase().replace("ğ","g").replace("ç","c").replace("ü","u").replace("ö","o").replace("ş","s")
    }

@yunusemre002
Copy link

Metindeki tüm (büyük ve küçük) türkçe karakterleri değiştirmek için tıklayınız.

@Furkan-Gulsen
Copy link

Bu şekilde de türkçe karakter sorununu çözebilirsiniz.

function decodeTurkishCharacters(text) {
  return text
    .replace(/\ğ/g, "g")
    .replace(/\ü/g, "u")
    .replace(/\ş/g, "s")
    .replace(/\ı/g, "i")
    .replace(/\ö/g, "o")
    .replace(/\ç/g, "c");
}

@ubeydeozdmr
Copy link

replace() yerine replaceAll() kullanmanız sorunu çözecektir.

Kod:

String.prototype.turkishtoEnglish = function () {
    return this.replace('Ğ','g')
        .replaceAll('Ü','u')
        .replaceAll('Ş','s')
        .replaceAll('I','i')
        .replaceAll('İ','i')
        .replaceAll('Ö','o')
        .replaceAll('Ç','c')
        .replaceAll('ğ','g')
 	.replaceAll('ü','u')
        .replaceAll('ş','s')
        .replaceAll('ı','i')
        .replaceAll('ö','o')
        .replaceAll('ç','c');
};

@Akif9748
Copy link

Daha performanslı bir yola geçtim:

const engChars = {
    "Ğ": "G",
    "Ü": "U",
    "Ş": "S",
    "İ": "I",
    "Ö": "O",
    "Ç": "C",
    "ğ": "g",
    "ü": "u",
    "ş": "s",
    "ı": "i",
    "ö": "o",
    "ç": "c"
};
const toEN=str => [...str].map(c => engChars[c] || c).join("");

@aydincandan
Copy link

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
}

@pemre
Copy link

pemre commented Apr 22, 2024

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