I have a function in which I'm converting an input string (arabic numeral) into an int value. I do this with the following function.
function convertToEnglishDigit(input) {
var numberMap = {
'۰': '0',
'۱': '1',
'۲': '2',
'۳': '3',
'۴': '4',
'۵': '5',
'۶': '6',
'۷': '7',
'۸': '8',
'۹': '9',
};
var result = parseInt(input.replace(/[۰-۹]/g, function(i) {
return numberMap[i];
}));
return result;
}
It works if the input is either a single character, such as '۲'
or if the string of characters are sequential such as '۱۲۳۴۵'
. If the input is a string of characters which are out of order such as "٢١٠"
the returned value of the replace()
is NaN
, why is this and how can I account for the situation in which they are out of order, which in my use case, will be almost always.