Skip to content

Instantly share code, notes, and snippets.

@amirasaran
Created September 27, 2016 06:23
  • Star 30 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save amirasaran/05f97748cd1d268dc3f6c85bdacb197b to your computer and use it in GitHub Desktop.
JavaScript Arabic character to Persian (تبدیل حروف عربی به فارسی)
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
String.prototype.toPersianCharacter = function () {
var string = this;
var obj = {
'ك' :'ک',
'دِ': 'د',
'بِ': 'ب',
'زِ': 'ز',
'ذِ': 'ذ',
'شِ': 'ش',
'سِ': 'س',
'ى' :'ی',
'ي' :'ی',
'١' :'۱',
'٢' :'۲',
'٣' :'۳',
'٤' :'۴',
'٥' :'۵',
'٦' :'۶',
'٧' :'۷',
'٨' :'۸',
'٩' :'۹',
'٠' :'۰',
};
Object.keys(obj).forEach(function(key) {
string = string.replaceAll(key, obj[key]);
});
return string
};
/**
* Example
*/
var string = 'ك ٤ ٦٦';
string = string.toPersianCharacter();
console.log(string); //out put "ک ۴ ۶۶"
@vbnetgenius
Copy link

thanks dude

@h2soheili
Copy link

thanks

@hosseinzamani91
Copy link

دستت درد نکنه داداش!

@H-sheibani
Copy link

thanks !

@sagho0or
Copy link

thanks dude

@farvisun
Copy link

nice

@mhmda-83
Copy link

خدا خیرت بده مومن

@smaznet
Copy link

smaznet commented Jul 27, 2021

خدا عمرتو زیاد کننه
دستت مرسی

@hadizz
Copy link

hadizz commented Dec 25, 2021

thanks❤️

@younesfmgtc
Copy link

const arabicToPersian = str => {
const diff = {
"ة": "ه",
"ك": "ک",
"دِ": "د",
"بِ": "ب",
"زِ": "ز",
"ذِ": "ذ",
"شِ": "ش",
"سِ": "س",
"ى": "ی",
"ي": "ی",
"٠": "۰",
"١": "۱",
"٢": "۲",
"٣": "۳",
"٤": "۴",
"٥": "۵",
"٦": "۶",
"٧": "۷",
"٨": "۸",
"٩": "۹",
}
for (const [key, value] of Object.entries(diff)) {
str.replaceAll(key, value)
}
return str
}

@younesfmgtc
Copy link

عملکرد بالا با ES6 کوتاه تر است

@zsh77
Copy link

zsh77 commented Jun 14, 2022

added 'ئ'

String.prototype.replaceAll = function(search, replacement) {
        var target = this;
        return target.replace(new RegExp(search, 'g'), replacement);
    };

String.prototype.toPersianCharacter =  function () {
    var string = this;
    var obj = {
        'ك' :'ک',
        'دِ': 'د',
        'بِ': 'ب',
        'زِ': 'ز',
        'ذِ': 'ذ',
        'شِ': 'ش',
        'سِ': 'س',
        'ى' :'ی',
        'ي' :'ی',
        'ئ' :'ی',
        '١' :'۱',
        '٢' :'۲',
        '٣' :'۳',
        '٤' :'۴',
        '٥' :'۵',
        '٦' :'۶',
        '٧' :'۷',
        '٨' :'۸',
        '٩' :'۹',
        '٠' :'۰',
    };

    Object.keys(obj).forEach(function(key) {
        string = string.replaceAll(key, obj[key]);
    });
    return string
};

@amnbhr
Copy link

amnbhr commented Apr 16, 2023

Thanks Amir
bellow snippet just a shorter

function convertArabicCharToPersian(str) {
  const charsMapping = {
    ك: "ک",
    دِ: "د",
    بِ: "ب",
    زِ: "ز",
    ذِ: "ذ",
    شِ: "ش",
    سِ: "س",
    ى: "ی",
    ي: "ی",
    ئ: "ی",
    "١": "۱",
    "٢": "۲",
    "٣": "۳",
    "٤": "۴",
    "٥": "۵",
    "٦": "۶",
    "٧": "۷",
    "٨": "۸",
    "٩": "۹",
    "٠": "۰",
  };

  return Object.keys(charsMapping).reduce((prev, curr) => {
    return prev.replaceAll(curr, charsMapping[curr]);
  }, str || "");
}

@javadmsd
Copy link

javadmsd commented Feb 2, 2024

Thanks bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment