How to convert Persian and Arabic numbers inside a string to English using JavaScript?

后端 未结 9 991
萌比男神i
萌比男神i 2020-12-05 04:15

How can I convert Persian/Arabic numbers to English numbers with a simple function?

arabicNumbers = [\"١\", \"٢\", \"٣\", \"٤\", \"٥\", \"٦\", \"٧\", \"٨\",          


        
相关标签:
9条回答
  • 2020-12-05 04:50

    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 with function(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 the g 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 use indexOf 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 us 3 because '۳' is the third index in the string '۰۱۲۳۴۵۶۷۸۹'
    0 讨论(0)
  • 2020-12-05 04:51

    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]
    
    0 讨论(0)
  • 2020-12-05 04:55

    Transforms any Persian or Arabic (or mixed) number to "English" numbers (Hindu–Arabic numerals)

    var transformNumbers = (function(){
        var numerals = {
            persian : ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"],
            arabic  : ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"]
        };
    
        function fromEnglish(str, lang){
            var i, len = str.length, result = "";
    
            for( i = 0; i < len; i++ )
                result += numerals[lang][str[i]]; 
    
            return result;
        }
    
        return {
            toNormal : function(str){
                var num, i, len = str.length, result = "";
    
                for( i = 0; i < len; i++ ){
                    num = numerals["persian"].indexOf(str[i]);
                    num = num != -1 ? num : numerals["arabic"].indexOf(str[i]);
                    if( num == -1 ) num = str[i];
                    result += num; 
                }
                  
                return result;
            },
    
            toPersian : function(str, lang){
                return fromEnglish(str, "persian");
            },
    
            toArabic : function(str){
                return fromEnglish(str, "arabic");
            }
        }
    })();
    
    //////// ON INPUT EVENT //////////////
    
    document.querySelectorAll('input')[0].addEventListener('input', onInput_Normal);
    document.querySelectorAll('input')[1].addEventListener('input', onInput_Arabic);
    
    function onInput_Arabic(){
       var _n = transformNumbers.toArabic(this.value);
       console.clear();
       console.log( _n )
    }
    
    function onInput_Normal(){
       var _n = transformNumbers.toNormal(this.value);
       console.clear();
       console.log( _n )
    }
    input{ width:90%; margin-bottom:1em; font-size:1.5em; padding:5px; }
    <input placeholder="write in Arabic numerals">
    
    <input placeholder="write in normal numerals">

    0 讨论(0)
提交回复
热议问题