Skip to content

Instantly share code, notes, and snippets.

@hbehkamal
Forked from AliMD/fix-persian-number.js
Last active August 29, 2015 14:25
Show Gist options
  • Save hbehkamal/75ba6db1fa5c912b1e6b to your computer and use it in GitHub Desktop.
Save hbehkamal/75ba6db1fa5c912b1e6b to your computer and use it in GitHub Desktop.

Use this simple function to convert your string

var
persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g],
arabicNumbers  = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g],
fixNumbers = function (str)
{
  if(typeof str === 'string')
  {
    for(var i=0; i<10; i++)
    {
      str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i);
    }
  }
  return str;
};

Example

var mystr = 'Sample text ۱۱۱۵۱ and ٢٨٢٢';
mystr = fixNumbers(mystr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment