Skip to content

Instantly share code, notes, and snippets.

@AliMD
Last active March 15, 2024 15:03
Show Gist options
  • 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

@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