Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert/Fix persian and arabic numbers inside a string to english in javascript, AliMD Edition ;)
(() => {
'use strict';
const
// Establish the root object, `window` (`self`) in the browser, `global`
// on the server, or `this` in some virtual machines. We use `self`
// instead of `window` for `WebWorker` support.
root = (() => {
if (typeof self == 'object' && self.self === self) return self;
else if (typeof global == 'object' && global.global === global) return global;
else return this;
})(),
persianNumberArr = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g],
arabicNumberArr = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g],
fixPersianNumber = str => {
if(typeof str === 'string')
{
let i = 0;
for(; i<10; i++)
{
str = str.replace(persianNumberArr[i], i).replace(arabicNumberArr[i], i);
}
}
return str;
};
root.fixPersianNumber = fixPersianNumber;
})();

Fix Persian Numbers AliMD Edition ;)

let mystr = 'Sample text ۱۱۱۵۱ and ٢٨٢٢';
mystr = fixPersianNumber(mystr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.