Skip to content

Instantly share code, notes, and snippets.

@AliMD
Last active March 15, 2024 15:03
Show Gist options
  • Star 53 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save AliMD/6439187 to your computer and use it in GitHub Desktop.
Save AliMD/6439187 to your computer and use it in GitHub Desktop.
Best Regular Expression for Detect Iranian Mobile Phone Numbers

Best Regular Expression for Detect Iranian Mobile Phone Numbers

I'm sure its best regular expression for detect iranian mobile number.

(0|\+98)?([ ]|,|-|[()]){0,2}9[1|2|3|4]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}


use in javascript

var
mobileReg = /(0|\+98)?([ ]|-|[()]){0,2}9[1|2|3|4]([ ]|-|[()]){0,2}(?:[0-9]([ ]|-|[()]){0,2}){8}/ig,
junkReg = /[^\d]/ig,
persinNum = [/۰/gi,/۱/gi,/۲/gi,/۳/gi,/۴/gi,/۵/gi,/۶/gi,/۷/gi,/۸/gi,/۹/gi],
num2en = function (str){
  for(var i=0;i<10;i++){
    str=str.replace(persinNum[i],i);
  }
  return str;
},
getMobiles = function(str){
  var mobiles = num2en(str+'').match(mobileReg) || [];
  mobiles.forEach(function(value,index,arr){
    arr[index]=value.replace(junkReg,'');
    arr[index][0]==='0' || (arr[index]='0'+arr[index]);
  });
  return mobiles;
};

// test
console.log(getMobiles("jafang 0 91 2 (123) 45-67 jafang or +۹۸ (۹۱۵) ۸۰ ۸۰ ۸۸۸"));

support all option like these
912 123 4567
912 1234 567
912-123-4567
912 (123) 4567
9 1 2 1 2 3 4 5 6 7
9 -1 (2 12))3 45-6 7
and all with +98 or 0
+989121234567
09121234567
9121234567
or even persian numbers +۹۸ (۹۱۵) ۸۰ ۸۰ ۸۸۸

and only detect true iranian operator numbers 091x 092x 093x 094x

@amingholami
Copy link

do u have any idea about static phone numbers of Iran?

@zamaniamin
Copy link

very Good! Damet garm dada

@mahdieftekhaari
Copy link

mahdieftekhaari commented Jul 24, 2017

thanks, very good... and now days "true iranian operator numbers" supports 090*

@Hamidnch
Copy link

Hamidnch commented Aug 26, 2017

Thanks.Last update for support the some of Irancell number.(for example: 0903 ------)
"^(0|+98)?([ ]|,|-|[()]){0,2}9[0|1|2|3|4]([ ]|,|-|[()]){0,3}(?:[0-9]([ ]|,|-|[()]){0,2}){8}$"
[Best Regards]

@alirezatahani
Copy link

that's greate but doesn't filter the 0923 , 0924, 0925 ....
and we don't have 094...

@ali-master
Copy link

Thanks, Sir

But your regex has ReDOS bug! please rewrite again your written regex.

Best regards.

@davodaslanifakor
Copy link

i test this by farsi digit but not match
this demo

@baznak
Copy link

baznak commented Jul 10, 2019

سلام
لطفا تغیرش بدید به این
(\+98|0|98|0098)?([ ]|-|[()]){0,2}9[0-9]([ ]|-|[()]){0,2}(?:[0-9]([ ]|-|[()]){0,2}){8}/
چون شماره های زیر پوشش نمیدی
0991.....
0903....
و موارد زیاد دیگه
اینی که دادم همه نوع رو پوشش میده

@h-ebrahimi
Copy link

hi
the best regex pattern

(+?98|098|0|0098)?(9\d{9})

@moostafaa
Copy link

Thanks for your effort, but this regex does not detect all Iranian mobile numbers, as you can see in this Link, MCI operator and Irancell operator added new formats that your regex does not support, I changed your regex for supporting this new numbers:

(0|+98)?([ ]|,|-|[()]){0,2}9[0|1|2|3|4|9]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}

@seyedmmousavi
Copy link

^(+989(\d{9})|09?(\d{9})|0098(9\d{9}))$

@alirezaseif28
Copy link

it,s very good https://gist.github.com/AliMD/6439187#gistcomment-2966025
.if number persian or arabic when not working.
you should use this code

function fixNumbers(str) {
     var persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g];
     var arabicNumbers = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g];

if (typeof str === 'string') {
    for (var i = 0; i < 10; i++) {
        str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i);
     }
 }
 return str; }; 

@bahramxi
Copy link

bahramxi commented Nov 17, 2022

that great

@Aghaie
Copy link

Aghaie commented Dec 25, 2022

Latest update:
support new prenumbers, support arabic keyboards:


var mobileReg = /(\+98|0|98|0098)?([ ]|-|[()]){0,2}9[0-9]([ ]|-|[()]){0,2}(?:[0-9]([ ]|-|[()]){0,2}){8}/ig,
junkReg = /[^\d]/ig,
persianNum = [/۰/gi,/۱/gi,/۲/gi,/۳/gi,/۴/gi,/۵/gi,/۶/gi,/۷/gi,/۸/gi,/۹/gi],
arabicNum = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g];

num2en = function (str){
  for(var i=0;i<10;i++){
    str=str.replace(persianNum[i],i).replace(arabicNum[i], i);
  }
  return str;
},
getMobiles = function(str){
  var mobiles = num2en(str+'').match(mobileReg) || [];
  mobiles.forEach(function(value,index,arr){
    arr[index]=value.replace(junkReg,'');
    arr[index][0]==='0' || (arr[index]='0'+arr[index]);
  });
  return mobiles;
};

// test
console.log(getMobiles("jafang 0 91 2 (123) 45-67 jafang or +۹۸ (۹۱٤) ۸۰ ۸۰ ۸۸۸"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment