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

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

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

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


        
9条回答
  •  Happy的楠姐
    2020-12-05 04:36

    Short and easy!

    "۰۱۲۳۴۵۶۷۸۹".replace(/([۰-۹])/g, function(token) { return String.fromCharCode(token.charCodeAt(0) - 1728); });
    

    Or in a more modern manner

    "۰۱۲۳۴۵۶۷۸۹".replace(/([۰-۹])/g, token => String.fromCharCode(token.charCodeAt(0) - 1728));
    

提交回复
热议问题