Skip to content

Instantly share code, notes, and snippets.

@amirasaran
Created September 27, 2016 06:23
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save amirasaran/05f97748cd1d268dc3f6c85bdacb197b to your computer and use it in GitHub Desktop.
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 "ک ۴ ۶۶"
@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

@miladhashemi
Copy link

Thanks Amir,
This is equivalent to code, in typescript:

export {};

declare global {
  interface String {
    replaceAll(
      search: string,
      replacement: string | ((substring: string, ...args: any[]) => string),
    ): string;
    toPersianCharacter(): string;
    toArabicCharacter(): string;
  }
}

String.prototype.replaceAll = function (
  search: string,
  replacement: string | ((substring: string, ...args: any[]) => string),
): string {
  return this.replace(new RegExp(search, 'g'), replacement);
};

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

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

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

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

Usage:

import './char-convertor.ts'
let text: string='میلاد';
const arabic = text.toArabicCharacter();
console.log(arabic);

@majid-rafei
Copy link

Thanks

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