For regular expressions, we might use them often, but it's hard to remember. This article, I will introduce the simple regular expression knowledge, and then summarize the front-end development of commonly used regular expression Daquan, need to refer to the friend
Objective
Just this thing, the feeling is that the development will come into contact with some.
Those so-called basic grammar will not say ha, a search a lot, to some practical.
I am here to share my work in the summary and encountered, I hope to some small partners have some help AH!!
Regular rollup
Match URL
Const REGEXURL =/((https?| FTP)?: \ /\/)? (www\.)? [-a-za-z0-9@:%._\+~#=] {2,256}\. [A-z] {2,6}\b ([-a-za-z0-9@:%_\+.~#?&//=]*)/gi;//this regular can match such a lump of url//www.baidu.com//http://www.baidu.com//https:/ /www.baidu.com//baidu.com//test.baidu.com //gds.baidu.com//ftp://www.baidu.com//----Split line----// Maybe there are small partners do not need to match such a big lump, as long as the match HTTP and HTTPS, the above rewrite a bit better. Const REGEXURL =/(https?:\ /\/) (www\.)? [-a-za-z0-9@:%._\+~#=] {2,256}\. [A-z] {2,6}\b ([-a-za-z0-9@:%_\+.~#?&//=]*)/gi;//http://www.baidu.com//https://www.baidu.com
Matching Chinese identity card not very reliable version
Why this is not true, because it is impossible to achieve regional combined birth date judgment this very precise recognition of the const Regexchinaidcard =/^ ((1[1-5)) | ( 2[1-3]) | (3[1-7]) | (4[1-6]) | (5[0-4]) | (6[1-5]) |71| (8[12]) |91) \d{4} ((19\d{2} (0[13-9]|1[012]) (0[1-9]|[ 12]\D|30)) | (19\d{2} (0[13578]|1[02]) 31) | (19\d{2}02 (0[1-9]|1\d|2[0-8)) | (19 ([13579][26]|[ 2468][048]|0[48]) (0229)) \d{3} (\d| x|x)? $/gi;//can match such a pile of identity cards, identity card numbers are network search, a search a bunch of//230381198104143414//650201199007186135//460106197707275739// 44200019860325932x//43052819880216450x//654223197502255401
Match numbers, integers, floats ~ ~ ~
The goods will only match the positive integer const Regexinteger =/^\d+$/gi;//Certainly someone wants to ask, the range of integers, see I changed, such as 0-100const Regexrangeinteger =/^\d$|^[1-9]\d$|^ 100$/gi;//matches decimals, the const REGEXFLOAT =/^\d+\.\d+$/gi//If you want to limit it, for example, or that, 0~100 any integer and floating point const REGEXRANGEINTFLOAT =/^ (\ d{0,2} (\.\d+)? | 100 (\.0+)?) $/gi;
Match phone number
Not including satellite phone these, just listed the common phone range, also considered the area and long distance prefix const Regexmobilenumber =/^ (0|86|17951)? (13[0-9]|15[012356789]|17[678]|18[0-9]|14[57]) [0-9] {8}$/gi;
Match mailbox
Const REGEXEMAIL =/^ (([^<> () \[\]\\.,;:\ s@ "]+ (\.[ ^<> () \[\]\\.,;:\ s@ "]+) *) | (".+")) @ (\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3}]) | ([a-za-z\-0-9_]+\.) +[a-za-z]{2,})) $/gi//has been tested in such a number of ways the format of the mailbox has passed the//cc011@qq.com//fsdlk@dd-fad.cn//df_fs2.q@jd-f.com// Crklej-dsfj@132.com//fdfkj@aa_fslkdfj.com.cn//d390.fslk@qq.com//1100@gmail.cn//sflk-98_dsf@qq_fsk.com
Enhanced Password Setup Specification
must contain uppercase and lowercase letters, numbers and special symbols, length 8~24const Regexenhancepassword =/^ (?! \s) ((? =.*[a-za-z]) (? =.*[a-z]) (? =.*[a-z]) (? =.*[\w_]). \s{8,24}) $/g;
China license plate number Check
Const Regexcarlicense =/^[Beijing tianjin Shanghai Yu Ji Yu Yun liao Black Xiang Wan lu xin su zhe gan e Guiganjin Meng Shan Gi Jingui Guangdong Qinghai-Tibet Kanningjong so that the collar a-z]{1}[a-z]{1}[a-z0-9]{4}[a-z0-9 hang Learning Police Hong Kong and Macao]{1}$/g;
Match QQ number, No.
QQ in my impression seems to have been a pure number, and now seems to have developed to 11, as if initially from the 100000 const Regexqqnumber =/^[1-9]\d{5,10}$/gi;//deliberately to check the next number of rules//1, Number is the only voucher, can only be set 1 times,//2, you can use 6-20 letters, numbers, underscores and minus signs;//3 must start with a letter (letters are not case sensitive);//4, do not support setting Chinese. Const REGEXWECHATNUMBER =/^[a-za-z][\w-]{5,19}$/gi;
User name Specification
Normal edition, English + digits, must start with letter, allow underscore, length eight to 16 const REGEXNORMALUSERNAME =/^[a-za-z]\w{7,15}$/gi;//On this basis allow Chinese four to six characters const REGEXE1 = /^[\u4e00-\u9fa5]{4,6}$|^[a-za-z]\w{7,15}$/gi;//multiple re-use version, allow mobile phone, mailbox, Chinese, this if written as a word, with a grouping of the wording is good//but very long,,, recommended with If...else ... To split, three regular ....
Summarize
I don't have a lot of weird regular stuff, which are common in my work,
The
All the regular are tested in real-world situations, not hypothetical inferences ....