Skip to content

Instantly share code, notes, and snippets.

@amirasaran
Created September 27, 2016 06:23
Show Gist options
  • 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 "ک ۴ ۶۶"
@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 || "");
}

@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

@parsagholipour
Copy link

parsagholipour commented Sep 19, 2024

const arabicToPersian = str => {
  if (!str) return str;

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

  for (const [key, value] of Object.entries(diff)) {
    str = str.replaceAll(key, value);
  }

  return str;
};

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