javascript - Convert only Arabic numbers to English in a text input - Stack Overflow

I have found this function which works perfectly when text input is only Arabic numbers:function parseA

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.

Share Improve this question edited Jul 25, 2019 at 11:33 Habib 9482 gold badges10 silver badges24 bronze badges asked Apr 5, 2017 at 8:08 Aless HosryAless Hosry 1,1894 gold badges22 silver badges54 bronze badges 1
  • Great! this is a good utility method for converting Persian and Arabic numbers to English – Ali Sherafat Commented Jun 27, 2017 at 11:38
Add a ment  | 

2 Answers 2

Reset to default 3

For 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745362781a4624436.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信