How to convert Persian and Arabic digits of a string to English using JavaScript?

Active3 hr before
Viewed126 times

7 Answers


Meta Stack Overflow , Stack Overflow help chat ,Stack Overflow en español,Stack Overflow на русском

Use this simple function to convert your string

   persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g],
   arabicNumbers = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g],
   fixNumbers = function(str) {
      if (typeof str === 'string') {
         for (var i = 0; i < 10; i++) {
            str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i);
      return str;


var mystr = 'Sample text ۱۱۱۵۱ and ٢٨٢٢';
mystr = fixNumbers(mystr);
load more v

I use the following utility method to convert Persian and Arabic digits to English using regex:,for convert Persian or Arabic number to English number:,Thanks for contributing an answer to Code Review Stack Exchange!,Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.

Be nice to the maintenance programmer, even (especially?) if you expect it to be you. If you're mixing characters which are visually indistinguishable but don't need to be literal self-representations, you can use Unicode escapes and hexadecimal offsets as so:

convertNumbers2English: function(string) {
   return string.replace(/[\u0660-\u0669]/g, function(c) {
      return c.charCodeAt(0) - 0x0660;
   }).replace(/[\u06f0-\u06f9]/g, function(c) {
      return c.charCodeAt(0) - 0x06f0;

If I care about squeezing every last byte out of my JS, I can see a way to combine the two into one:

convertNumbers2English: function(string) {
   return string.replace(/[\u0660-\u0669\u06f0-\u06f9]/g, function(c) {
      return c.charCodeAt(0) & 0xf;
load more v

let mystr = 'Sample text ۱۱۱۵۱ and ٢٨٢٢';
mystr = fixPersianNumber(mystr);

const e2p = s => s.replace(/\d/g, d => '۰۱۲۳۴۵۶۷۸۹' [d])
const e2a = s => s.replace(/\d/g, d => '٠١٢٣٤٥٦٧٨٩' [d])

const p2e = s => s.replace(/[۰-۹]/g, d => '۰۱۲۳۴۵۶۷۸۹'.indexOf(d))
const a2e = s => s.replace(/[٠-٩]/g, d => '٠١٢٣٤٥٦٧٨٩'.indexOf(d))

const p2a = s => s.replace(/[۰-۹]/g, d => '٠١٢٣٤٥٦٧٨٩' ['۰۱۲۳۴۵۶۷۸۹'.indexOf(d)])
const a2p = s => s.replace(/[٠-٩]/g, d => '۰۱۲۳۴۵۶۷۸۹' ['٠١٢٣٤٥٦٧٨٩'.indexOf(d)])

e2p("asdf1234") // asdf۱۲۳۴
e2a("asdf1234") // asdf١٢٣٤
p2e("asdf۱۲۳۴") // asdf1234
a2e("asdf١٢٣٤") // asdf1234
p2a("asdf۱۲۳۴") // asdf١٢٣٤
a2p("asdf١٢٣٤") // asdf۱۲۳۴
load more v

I use below code to type number in persian into textbox:,If you want to insert only english number based on your textbox value(persian) to database then use the below method.,If you want to change persian number to english on key press then use the below code.,now I want it insert number in english into databse:

I use below code to type number in persian into textbox:

       $(function() {
          $('[id*=TxtTsabet]').keyup(function(e) {
             var ctrlKey = 67,
                vKey = 86;
             if (e.keyCode != ctrlKey && e.keyCode != vKey) {

       function toPersianNumber(input) {
          var inputstring = input;
          var persian = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"]
          for (var j = 0; j < persian.length; j++) {
             inputstring = inputstring.toString().replace(new RegExp(j, "g"), persian[j]);

          return inputstring;
load more v

What is 'CodeProject'?,Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.,View Unanswered Questions, help? What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum About Us

function numentofa(n) {
   var digits = [],
   do {
      r = n % 10;
      n = (n - r) / 10;
      digits.unshift(String.fromCharCode(r + 1776));
   } while (n > 0);
   return digits.join('');

window.onload = aa();

function aa() {
   var oooook = numentofa("121");
load more v

A simple JavaScript library To use the most commonly used string operations. Include functions of persian.js,Used for converting Persian and arabic Number to English numbers.,Used for converting Arabic and Persian numbers to English.,Used for converting Arabic and English numbers to Persian.

const starkString = require("starkstring");


import starkString from "starkstring";
load more v

Other "using-string" queries related to "How to convert Persian and Arabic digits of a string to English using JavaScript?"