I have found this function which works perfectly when text input is only Arabic numbers:
function parseArabic(){ // PERSIAN (فارسی), ARABIC (عربي) , URDU (اُردُو)
var yas ="٠١٢٣٤٥٦٧٨٩";
yas = Number(yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) {
return d.charCodeAt(0) - 1632;
}).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; })
);
alert(yas);
}
Here the alerted value of yas is "0123456789".Ok great but now how can I use this function when I have other characters in the same variable that could be english(letters and numbers) and arabic (letters)? Ex: "test ٠١٢٣٤٥٦٧٨٩ hello مرحبا "
. I am asking this because parseArabic accepts only arabic numbers to be translated; others are considered as NaN.
I have found this function which works perfectly when text input is only Arabic numbers:
function parseArabic(){ // PERSIAN (فارسی), ARABIC (عربي) , URDU (اُردُو)
var yas ="٠١٢٣٤٥٦٧٨٩";
yas = Number(yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) {
return d.charCodeAt(0) - 1632;
}).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; })
);
alert(yas);
}
Here the alerted value of yas is "0123456789".Ok great but now how can I use this function when I have other characters in the same variable that could be english(letters and numbers) and arabic (letters)? Ex: "test ٠١٢٣٤٥٦٧٨٩ hello مرحبا "
. I am asking this because parseArabic accepts only arabic numbers to be translated; others are considered as NaN.
- Great! this is a good utility method for converting Persian and Arabic numbers to English – Ali Sherafat Commented Jun 27, 2017 at 11:38
2 Answers
Reset to default 3For some one who does not want to take the pains of finding Number
(as per OP's answer). Here is the updated code :
function parseArabic(){ // PERSIAN (فارسی), ARABIC (عربي) , URDU (اُردُو)
var yas ="٠١٢٣٤٥٦٧٨٩";
yas = (yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) {
return d.charCodeAt(0) - 1632;
}).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; })
);
alert(yas);
}
The shortest and simple method to convert Arabic-Eastern numbers (including Persian numbers) to Arabic Western (Latin) numbers is the following short line of code.
This will also replace the Arabic decimal point "fasilah" if any with the required Latin decimal dot marker.
Only numbers are replaced but other text remains unchanged.
const numArFaToLatin=n=>n.replace(/[٠-٩۰-۹]/g,n=>15&n.charCodeAt(0)).replace(/٫/g,".").replace(/٬/g,"");
// ============== Test Cases =================
console.log(numArFaToLatin("۱۶٦٧۶۰")); // '166760'
console.log(numArFaToLatin("With decimal ۱۶۴٫۵۶")); // With decimal 164.56
console.log(numArFaToLatin("٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹")); // '01234567890123456789'
console.log(numArFaToLatin("Total is ١٢٣٤٠١")); // 'Total is 123401'
console.log(numArFaToLatin("With decimal ١٬٢٣٤٫٠١")); // 'Total is 1234.01'
console.log(numArFaToLatin("These are Numbers: ٠١٢٣٤٥٦٧٨٩")); // 'These are Numbers: 0123456789'
console.log(numArFaToLatin("العدد واحد ١١١١١١ فقط ")); // 'العدد واحد 111111 فقط '
console.log(numArFaToLatin("١٩,٢٣٤٫٥٦")); // 19,234.56