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

后端 未结 9 1007
萌比男神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:48

    If the string may contain both "Arabic" and "Persian" numbers then a one-line "replace" can do the jo as follows.

    The Arabic and Persian numbers are converted to English equivalents. Other text remains unchanged.

    Num= "۳٣۶٦۵any٥۵٤۶32٠۰";     // Output should be "33665any55453200"
    
    Num = Num.replace(/[٠-٩]/g, d => "٠١٢٣٤٥٦٧٨٩".indexOf(d)).replace(/[۰-۹]/g, d => "۰۱۲۳۴۵۶۷۸۹".indexOf(d));
    
    console.log(Num);

提交回复
热议问题