Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
4.78/5 (2 votes)
I am trying to use this function to replace only Arabic numbers in text input:

function parseArabic(){ 
     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);
}


The alerted yas is "0123456789" which is great. Now if I use yas ="٠١٢٣٤٥٦٧٨٩ g", NaN is alerted . So this function works in case we have only Arabic numbers. So I need a function like this one to replace all Arabic numbers by their values but other characters should stay as they are ...

What I have tried:

I don't wan't to replace each arabic number alone, example: replace(٠ by 0) and
replace (١ by 1) ...
Posted
Updated 4-Apr-17 22:51pm
v2

1 solution

remove the Number function and try

Quote:
So I need a function like this one to replace all Arabic numbers by their values but other characters should stay as they are

JavaScript
var yas = "٠١٢٣٤٥٦٧٨٩ g"; 
    yas = yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) { return d.charCodeAt(0) - 1632; })
        .replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; });
    alert(yas);// "0123456789 g"
 
Share this answer
 
v3
Comments
H.AL 5-Apr-17 5:01am    
you saved my day, Thank you
Karthik_Mahalingam 5-Apr-17 5:02am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900