How to convert Persian and Arabic digits of a string to English using JavaScript?
How can I convert Persian/Arabic numbers to English numbers with a simple function?
arabicNumbers = ["١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩", "٠"]
persianNumbers = ["۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "۰"]
It is the same schema, but the code pages are different.
Solution 1:
Use this simple function to convert your string
var
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;
};
Be careful, in this code the persian numbers codepage are different with the arabian numbers.
Example
var mystr = 'Sample text ۱۱۱۵۱ and ٢٨٢٢';
mystr = fixNumbers(mystr);
Refrence
Solution 2:
Oneliner of all 6 possible translations between English, Arabic, and persian Digits.
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۱۲۳۴
Explaination:
-
(s => f(s))(x)
is a lambda function that is immediately executed, and will be equal to f(x) -
s.replace(pattern, function)
looks for matches of pattern in s, for every match m it will replace m withfunction(m)
in the string. -
/\d/g
is a regex pattern,\d
means a digit in the English language,g
means global. If you don't specify theg
it will only match the first occurrence, otherwise it will match all the occurrences. - In this case for every English digit
d
in the string, that digit will be replaced by'۰۱۲۳۴۵۶۷۸۹'[d]
so, 3 will be replaced by the third index in that list('۰۱۲۳۴۵۶۷۸۹'
) which is '۳' -
/[۰-۹]/g
is the equivalent regex for Persian digits this time we can't use the same method, before we took advantage of the fact that javascript is dynamically typed and that d is automatically converted from a string(regex match) to a number(array index) (you can do'1234'['1']
in javascript which is the same as'1234'[1]
) - but this time we can't do that because
'1234'['۱']
is invalid. so we use a trick here and useindexOf
which is a function that tells us the index of an element in an array(here a character in a string) so,'۰۱۲۳۴۵۶۷۸۹'.indexOf(۳)
will give us3
because'۳'
is the third index in the string'۰۱۲۳۴۵۶۷۸۹'
Solution 3:
this is a simple way to do that:
function toEnglishDigits(str) {
// convert persian digits [۰۱۲۳۴۵۶۷۸۹]
var e = '۰'.charCodeAt(0);
str = str.replace(/[۰-۹]/g, function(t) {
return t.charCodeAt(0) - e;
});
// convert arabic indic digits [٠١٢٣٤٥٦٧٨٩]
e = '٠'.charCodeAt(0);
str = str.replace(/[٠-٩]/g, function(t) {
return t.charCodeAt(0) - e;
});
return str;
}
an example:
console.log(toEnglishDigits("abc[0123456789][٠١٢٣٤٥٦٧٨٩][۰۱۲۳۴۵۶۷۸۹]"));
// expected result => abc[0123456789][0123456789][0123456789]