Many friends who do not understand the regular, when meeting the need to use the regular verification data, are often looking for a long time on the Internet, and the results are still not very consistent with the requirements. So I recently put together a list of regular expressions used in development, including checksums, characters, special requirements, and so on. Keep a bottom line for yourself and your friends.

One, the expression of the check number

Digital: ^ [0-9] $n bit Numbers: ^ \ d {n} $at least n bit Numbers: ^ \ d {n,} $m – n bit Numbers: ^ \ d {m, n} $the number of zero and non-zero start: ^ (0 | [1-9] [0-9]) $nonzero at the beginning of the most number with two decimal places: ^ ((1-9] [0-9]) + (. [0-9] {1, 2})? $positive or negative number with 1-2 decimal places: ^(-)? \ d + (. \ d {1, 2})? $positive, negative, and decimal: ^ (+) – |? \d+(.\d+)? $positive real number with two decimal places: ^[0-9]+(.[0-9]{2})? $positive real numbers with 1 to 3 decimal places: ^[0-9]+(.[0-9]{1,3})? $non-zero positive integer: ^[1-9]\d$or ^([1-9][0-9]){1,3}$or ^+? [1-9] [0-9] $nonzero negative integer: ^ – 1-9 [] [] 0-9 “$or ^ – [1-9] \ d: $non-negative integer or ^ ^ \ d + $1-9] [\ d | 0 $a positive integer: ^ – [1-9] \ | 0 $d or ^ ((\ d +) | (0 +)) $nonnegative floating-point number: ^\d+(.\d+)? $or ^ [1-9] \ d \ d | 0. \ [1-9] d \ | 0 d? . | 0 + 0 $is a floating point number: ^ ((\ d + 1 (\ d +)?) | ((. 0 0 + +)?) ) $or ^ (- (1-9] [\ d \ d | 0. \ [1-9] d/d)) | 0? . | 0 + 0 $are floating point Numbers: ^ 1 – [9] \ d \ d | 0. \ [1-9] \ d d $or ^ (([0-9] +. [0-9] [0-9] [1-9]) | ([0-9] [1-9] [0-9] [0-9] +) | ([0-9] [1-9] [0-9])) $negative floating point Numbers: ^ – (1-9] [\ d \ d | 0. \ [1-9] d/d) $or ^ (- (([0-9] +. [0-9] [0-9] [1-9]) | ([0-9] [1-9] [0-9] [0-9] +) | ([0-9] [1-9] [0-9]))) $floating point Numbers: ^ (-? \d+)(.\d+)? $or ^ -? ([1-9]\d.\d|0.\d[1-9]\d|0? . | 0 + 0) $

Second, the expression of the verification character

Chinese characters: ^ [\ u4e00 – \ u9fa5] {0} $and Numbers in English: ^ [A Za – z0-9] + $or ^ [A Za – z0-9] 40 {4} $length is 3-20 all characters: ^. {3, 20} $string composed of 26 English letters: ^[A-ZA-z]+$The character string consists of 26 uppercase letters: ^[A-z]+$The character string consists of 26 lowercase letters: ^[A-z]+$The character string consists of 26 digits and 26 letters: ^[A-Za-Z0-9]+$The character string consists of 26 digits, 26 letters, or underscores: ^\w+$or ^\w{3,20}$: ^[\ e00 -\ u9fa5a-za-z0-9_]+$: ^[\u4E00-\ u9fa5a-z0-9_]+$: ^ [\ u4E00 – \ u9FA5A – Za – z0-9] + $or ^ [\ u4E00 – \ u9FA5A – Za – z0-9] {2, 20} $can input contains & ^ % ‘,; =? $\ “and other characters: [^%&’,;=?$\x22]+ Do not enter characters containing ~ : [^~\x22]+

Special requirements expression

Email address: ^ \ w + (+ / – +. \ w) + @ \ w ([-] \ w +) \ w + ([-] \ w +) $domain: [a zA – Z0-9] [9] – a – zA – Z0 – on conversion {0} (/. [a zA – Z0-9] [9] – a – zA – Z0 – on conversion {0}) + /.? InternetURL: [a zA – z] + : / / [^ \ s] or ^ http:// ([-] \ w + \.) +[\w-]+(/[\w-./?%&=]*)? $Mobile Phone Number: ^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$ Telephone number (” XXXX-XXXXXXX “, “XXXX-XXXXXXXX”, “XXXX-XXXXXXX”, “XXXX-XXXXXXX”, “XXXXXXX” and “XXXXXXXX”) : ^(? -) | \ \ d {3, 4} d} {3.4 -)? \ d {7, 8} $home phone number (0511-4405222, 021-87888822) : \ d {3} – \ d {8} | \ d {4} – \ d {7} id number (15, 18 digits) : ^ \ d {15} | \ d {and} $short id number (Numbers, the letter “x” at the end) : ^ ([0-9] {7} 16 (x | x)? $or ^ \ d {8} 16 | x [0-9] {8} 16 | x [0-9] {8} 16? $specifies whether the account name is valid (starts with a letter and contains 5-16 bytes and alphanumeric underscores) : ^[a-za-z][a-za-z0-9_]{4,15}$password (starts with a letter and must contain 6 to 18 letters, digits, and underscores) : ^[a-za-z]\w{5,17}$strong password (must contain uppercase and lowercase letters and digits, cannot use special characters, between 8 and 10 characters in length) : ^(? =.\d)(? =.[a-z])(? =. [a-z]). 8, 10 {} $date format: ^ \ d {4} \ d {1, 2} – \ d {1, 2} 12 months of the year (01 ~ 09 and 1 ~ 12) : ^ (0? [1-9] | 1 [2-0]) $a month of 31 days (01 ~ 09 and 1 ~ 31) : ^ ((0? [1-9]) | | 2 (1) ([0-9]) | | 30 31) $money input formats: there are four kind of money we can accept representation: “10000.00” and “10000.00,” and not “points” of “10000” and “10000” : ^ 1 – [9] [0-9] $that either don’t begin with 0, but it also means that a character “0” is not through, so we use the form below: ^ (0 | 1 – [9] [0-9]) $a 0 or a digital does not begin with zero. We can also allow the beginning there is a minus sign: ^ (0 | -? [1-9][0-9])$This represents a 0 or a potentially negative number that does not begin with a 0. Let the user start with 0. Let’s get rid of the minus, because money can’t be negative. ^[0-9]+(.[0-9]+)? $must state that there should be at least one digit after the decimal point, so “10.” will not pass, but “10” and “10.2” will pass: ^[0-9]+(.[0-9]{2})? ^[0-9]+(.[0-9]{1,2}) ^[0-9]+(.[0-9]{1,2}) This allows the user to write only one decimal digit. Below we should consider a comma in the digital, we can be like this: ^ [0-9] {1, 3} ([0-9] {3}) (. [0-9] {1, 2})? The $1 to 3 number, followed by any comma + 3 Numbers, comma become optional, rather than having to: ^ ([0-9] + | [0-9] {1, 3} ([0-9] {3})) (. [0-9] {1, 2})? $Note: this is the final result, don’t forget “+” can be replaced by “*”. If you think empty strings are acceptable (strange, why?) And finally, don’t forget to get rid of that backslash when you’re using a function, that’s where the usual mistakes are

^([a-za-z]+-?) + [a zA – Z0-9] + \. [x] | x | m [m] [l] | l $Chinese characters of regular expressions: [\ u4e00 – \ u9fa5] double-byte characters: [^\x00-\ XFF] (including Chinese characters, which can be used to calculate the length of a string (a two-byte character is 2, ASCII character 1)) regular expression for blank lines: \n\s\r (which can be used to delete blank lines) Regular expression for HTML tags: <(\ s?) [^ >] >.? . < / 1 > \ |
< span style = “box-sizing: border-box; color: RGB (74, 74, 74); ^ \ s | \ s $or (^ \ s) | (\ s * $) (can be used to delete rows first line of white space characters (including Spaces, tabs, form-feed character, etc.), very useful expressions) tencent QQ number: [1-9] [0-9] {4} (tencent QQ number since 10000) China zip code: [1-9] \ d {5} (? ! \ d) (China postal code is 6 digits) IP address: \ d +. \ d +. \ d +. \ d + (to extract the useful IP address) IP address: ((? : (? :25[0-5]|2[0-4]\d|[01]? \d? \d)\.) {3} (? :25[0-5]|2[0-4]\d|[01]? \d? \d)) (provided by @Feilongsshao, thanks for sharing)