Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save faridv/65fe4b30b6666acac161d7f054b5d375 to your computer and use it in GitHub Desktop.
Save faridv/65fe4b30b6666acac161d7f054b5d375 to your computer and use it in GitHub Desktop.
Convert Persian & English digits together | Javascript
String.prototype.toPersianDigits = function () {
var id = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
return this.replace(/[0-9]/g, function (w) {
return id[+w];
});
};
String.prototype.toEnglishDigits = function () {
var id = { '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4', '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9' };
return this.replace(/[^0-9.]/g, function (w) {
return id[w] || w;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment