JavaScript Common Regular Expression instances
Instance Source
1 varMyregexp = {2 //Check if the string is a legitimate QQ number3ISQQ:function(str) {4 //1 First position cannot be 0 ^[1-9]5 //2 must be a number of [5, 11] bits \d{4, 9}6 varReg =/^[1-9][0-9]{4,9}$/Gim;7 if(Reg.test (str)) {8Console.log (' QQ number format input correct '));9 return true;Ten}Else { OneConsole.log (' Please enter the correct format of the QQ number '); A return false; - } - }, the //Check if the string is a legitimate phone number -Isphone:function(str) { - varReg =/^ (0|86|17951)? (13[0-9]|15[012356789]|18[0-9]|14[57]|17[678]) [0-9] {8}$/; - if(Reg.test (str)) { +Console.log (' mobile phone number format input correct ')); - return true; +}Else { AConsole.log (' Please enter the correct format of the mobile phone number '); at return false; - } - }, - //Check if the string is a legitimate email address -Isemail:function(str) { - varReg =/^\w+ ((-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ ((\.| -) [a-za-z0-9]+] *\. [a-za-z0-9]+$/; in //var reg =/\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) */; - if(Reg.test (str)) { toConsole.log (' email format input correct ')); + return true; -}Else { theConsole.log (' Please enter the correct format of email '); * return false; $ }Panax Notoginseng }, - //checks if a string is a number theIsnumber:function(str) { + varReg =/^\d+$/; A if(Reg.test (str)) { theConsole.log (str + ' is number ')); + return true; -}Else { $Console.log (str + ' not a number ')); $ return false; - } - }, the //remove front and back spaces -Trimfunction(str) {Wuyi varReg =/^\s+|\s+$/G; the returnStr.replace (Reg, "); - }, Wu //Check if string exists in Chinese -Ischinese:function(str) { About varReg =/[\u4e00-\u9fa5]/GM; $ if(Reg.test (str)) { -Console.log (str + ' exists in Chinese '); - return true; -}Else { AConsole.log (str + ' does not exist in Chinese ')); + return false; the } - }, $ //Check whether a string is a legitimate postal code theIspostcode:function(str) { the //The starting number cannot be 0, then 5 digits [1-9]\d{5} the varReg =/^[1-9]\d{5}$/G; the //var reg =/^[1-9]\d{5} (?! \d) $/; - if(Reg.test (str)) { inConsole.log (str + ' is a valid ZIP code format ')); the return true; the}Else { AboutConsole.log (str + ' is not a valid postal code format ')); the return false; the } the }, + //Check if the string is a legitimate ID number -Isidcard:function(str) { the varReg =/^ (^[1-9]\d{7} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3}$) | (^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) ((\d{4}) |\d{3}[xx]) $) $/;Bayi if(Reg.test (str)) { theConsole.log (str + ' is a valid ID number ')); the return true; -}Else { -Console.log (str + ' is an illegal ID number ')); the return false; the } the }, the //checks if a string is a legitimate URL -Isurl:function(str) { the varReg =/^https?:\ /\/([a-za-z0-9_-]) + (\.)?) * (: \d+)? (\/((\.)? (\?)? =?&? [a-za-z0-9_-] (\?)?) *)*$/i; the if(Reg.test (str)) { theConsole.log (str + ' is a valid URL ');94 return true; the}Else { theConsole.log (str + ' is not a valid URL '); the return false;98 } About }, - //checks if the string is a valid date format Yyyy-mm-dd101IsDate:function(str) {102 varReg =/^[1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9]$/;103 if(Reg.test (str)) {104Console.log (str + ' is a valid date format ')); the return true;106}Else {107Console.log (str + ' is not a valid date format, Yyyy-mm-dd ');108 return false;109 } the },111 //checks if a string is a legitimate IP address theIsIP:function(str) {113 //1.1.1.1 four segment [0, 255] the //the first paragraph cannot be 0 . the //each segment cannot start with 0 the //117 //Native ip:58.50.120.18 Hubei province Jinzhou Telecom118 varreg =/^ ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) (\. ( [0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])) {3}$/gi;119 if(Reg.test (str)) { -Console.log (str + ' is a legitimate IP address '));121 return true;122}Else {123Console.log (str + ' is an illegal IP address '));124 return false; the }126 }127 } - //Test129 //Console.log (myregexp.isqq (' 80583600 ')); the //Console.log (Myregexp.isphone (' 17607160722 '));131 //Console.log (Myregexp.isemail (' [email protected] ')); the //Console.log (Myregexp.isnumber (' 100a '));133 //Console.log (Myregexp.trim (') ');134 //Console.log (Myregexp.ischinese (' baixiaoming '));135 //Console.log (Myregexp.ischinese (' xiaoming '));136 //Console.log (Myregexp.ispostcode (' 412345 '));137 //Console.log (Myregexp.isidcard (' 42091119940927001X '));138 //Console.log (Myregexp.isurl (' https://www.baidu.com/'));139 //Console.log (myregexp.isdate (' 2017-4-4 ')); $ //Console.log (Myregexp.isip (' 1.0.0.0 '));
"JavaScript" JavaScript common regular expression instances