最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论