Skip to content

Instantly share code, notes, and snippets.

@MeTe-30
Created December 6, 2016 07:53
Show Gist options
  • Save MeTe-30/ffb289b89af8c11ca6e1e7091cc8c129 to your computer and use it in GitHub Desktop.
Save MeTe-30/ffb289b89af8c11ca6e1e7091cc8c129 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