Collection of common regular expressions

skill 2 years ago Lao Li next door Last edited on 2022-11-12 14:13:17

First recommend a regular expression visualization gadget: https://tool.yeelz.com/devtool/37.html

Students with difficulties in regular reading can use this tool to visualize regular reading, and love reading regular

Number correlation

Mobile number (beginning with 1): /^(?:(?:\+|00)86)? 1\d{10}$/

Mobile number (beginning with 13 to 19): /^(?:(?:\+|00)86)? 1[3-9]\d{9}$/

Mobile number (beginning with the mobile number segment published by the Ministry of Industry and Information Technology): /^(?:(?:\+|00)86)? 1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/

Domestic fixed line number: /\d {3}- \d{8}|\d {4}- \d{7}/

Mailbox No.: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\. \w+([-.]\w+)*$/

Postal Code: /[1-9]\d{5}(?!\d)/

ID number: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/

Bank card number (public and private accounts): /^[1-9]\d{9,29}$/

license plate number: /^[Beijing, Tianjin, Shanghai, Chongqing, Hebei, Henan, Yunnan, Liaoning, Heilongjiang, Hunan, Anhui, Shandong, New Jiangsu, Zhejiang, Jiangxi, Hubei, Guangxi, Gansu, Shanxi, Inner Mongolia, Shaanxi, Jilin, Fujian, Guizhou, Guangdong, Qinghai, Tibet, Sichuan, Ningxia and Hainan envoys] [A-HJ-NP-Z] [A-HJ-NP-Z0-9] {4,5} [A-HJ-NP-Z0-9 linked to student police in Hong Kong and Macao]$/

QQ No.: /^[1-9][0-9]{4,10}$/

wechat number: /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/

Version number (x.y.z): /^\d+(?:\.\d+){2}$/

Legal account No. 1 (beginning with letter, 5-16 digits, alphanumeric underline allowed): /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/

Legal account number 2 (4-16 digits, letters, numbers, underscores, minus signs allowed): /^[a-zA-Z0-9_-]{4,16}$/

Strong password 1 (must contain a combination of uppercase and lowercase letters and numbers, cannot use special characters, and the length is between 8-10): /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]). {8,10}$/

Strong password 2 (must contain letters, numbers, and special characters: * * @ # $% ^&`~() -+=*): /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*~()-+=]+$)(?![a-z0-9]+$)(?![a-z\\W_!@#$%^&*~()-+=]+$)(?![0-9\W_!@#$%^&*~()-+=]+$)[a-zA-Z0-9\\W_!@#$%^&*~()-+=]/

website: /^(((ht|f)tps?):\/\/)? (^!@#$%^&*?.\s-?\.)+[a-z]{2,6}\/?/

Website with port number: /^((ht|f)tps?:\/\/)? [\w-]+(\.[\w-]+)+:\d{1,5}\/?$/

ip-v4: /\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.) {3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b/

ip-v6: /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6} )|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.) {3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.) {3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/

Digital correlation

Only numbers: /^[0-9] * $/or/^ d {1,}$/

Integer: /^-? [0-9]\d*$/

Positive integer: /^\+? [1-9]\d*$/

Non positive integer: /^-[1-9]\d*|0$/

negtive integer: /^-[1-9]\d*$/

Non negative integer: /^\d+$/

Floating point number: /^(-?\d+)(\.\d+)?$/

Positive floating point number: /^[1-9]\d*\. \d*|0\. \d*[1-9]\d*$/

Negative floating point number: /^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)/

Decimal: /^-? \d+\. \d+$/

Positive/Negative/Decimal: /^(\-|\+)? \d+(\.\d+)?$/

Positive real numbers shall be reserved to 2 decimal places: /^[0-9]+(.[0-9]{2})?$/

Positive real numbers shall be reserved for 1 to 3 decimal places: /^[0-9]+(.[0-9]{1,3})?$/

N digits: /^\d{n}$/

At least n digits: /^\d{n,}$/

Digits from m to n: /^\d{m,n}$/

Numbers and letters contain at least one of them: /^[A-Za-z0-9]+$/

Must contain numbers and letters: /^(?=.*[a-zA-Z])(?=.*\d).+$/

Md5 value: /^([a-f\d]{32}|[A-F\d]{32})$/

Base64 value: /^\s*data:(?:[a-z]+\/[a-z0-9-+.]+(?:;[a-z-]+=[a-z0-9-]+)?)? (?:;base64)?, ([a-z0-9!$&',()*+;=\-._~:@/?%\s]*?) \s*$/i

Character correlation

Characters from m to n bits: /^. {3,20}$/

English alphabetic characters: /^[A-Za-z]+$/

Uppercase alphabetic characters: /^[A-Z]+$/

Lower case alphabetic characters: /^[a-z]+$/

chinese characters: /^[\u4e00-\u9fa5]{0,}$/

Full width symbol: /[\uFF00-\uFFFF]/

Half width symbol: /[\u0000-\u00FF]/

At least one of Chinese characters, English, numbers and underscores: /^[\u4E00-\u9FA5A-Za-z0-9_]+$/

Does not contain the character "~": /[^~\x22]+/

Continuous repetition of characters: /(.)\1+/

Words without abc: /\b((?!abc)\w)+\b/

Time dependent

24-hour clock (HH: mm: ss): /^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/

12 hour clock (hh: mm: ss): /^(?:1[0-2]|0?[1-9]):[0-5]\d:[0-5]\d$/

24-hour clock time (HHmmss): /([0-1]?[0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$/

Date 1 (yyyy MM dd, such as 2222-01-01, the year must be 4 digits): /^\d {4}- \d {1,2}- \d{1,2}/

Date 2 (e.g. 333-01-01, the year can be less than 4 digits): /^\d{1,4}(-)(1[0-2]|0?[1-9])\1(0?[1-9]|[1-2]\d|30|31)$/

Date 3 (yyyyMMdd, e.g. 20220202): /^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))$/

Date+Time 1 (YYYYMMDDHH: mm: ss): /^\d{4}([/:-\S])(1[0-2]|0?[1-9])\1(0?[1-9]|[1-2]\d|30|31)(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/

Date+Time 2: /^[1-9]\d {3}- (0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/

12 months a year (01~09 or 1~12): /^(0?[1-9]|1[0-2])$/

31 days a month (01~09 or 1~31): /^((0?[1-9])|((1|2)[0-9])|30|31)$/

Months with 31 days: /^(0?[13578]|1[02])$/

There are 30 days and months: /(0[469]|11)-(0[1-9]|[12][0-9]|30)/

February 28 ("02-28"): /^02-(0[1-9]|[1][0-9]|2[0-8])$/

leap year: /^(((19|20)([13579][26]|[2468][048]|0[48]))|(2000))$/

February of leap year (such as 2008-02-01): /^(((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0? 2-(0?[1-9]|[12]\d)$/

Date (including leap year, big and small month): /((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0? 2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0? 2-29))$/

Year range - year (e.g. 19 to 20 years): /^((19|20)\d{2})$/

Year range - month (such as January 1999): /^((((19|20)\d{2})-(0?[13-9]|1[012]))|(((19|20)\d{2})-(0?[13578]|1[02]))|(((19|20)\d{2})-0? 2)|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0? 2))$/

Year range - MM/DD/YYYY (such as January 1, 1999): /^((((19|20)\d{2})-(0?[13-9]|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0? 2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0? 2-29))$/.test('2021-02-21')$/

Year range - year month day (interval symbol can be -/or empty): /^(?:(?:1[6-9]|[2-9][0-9])[0-9]{2}([-/.]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:(?:1[6-9]|[2-9][0-9])(?:0[48]|[2468][048]|[13579][26])|(?:16|[2468][048]|[3579][26])00)([-/.]?)0? 2\2(?:29))$/

Programming related

Hexadecimal color: /^#? ([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/

Extract web page color code: /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/

Video link address: /^https?:\/\/ (.+\/)+.+ (\.(swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4))$/i

Picture link address: /^https?:\/\/ (.+\/)+.+ (\.(gif|png|jpg|jpeg|webp|svg|psd|bmp|tif))$/i

Mac address: /^((([a-f0-9]{2}:){5})|(([a-f0-9] {2}- ){5}))[a-f0-9]{2}$/i

Subnet mask: /^((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.) {3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))$/

File extension validation: /^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$/

Java package name (x.x.x): /^([a-zA-Z_]\w*)+([.][a-zA-Z_]\w*)+$/

Xml file: /^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\. [x|X][m|M][l|L]$/

Html comment: /<!-- [\s\S]*?-->/ g

Html tag 1: /<(\w+)[^>]*>(.*?<\/\1>)?/

Html tag 2: /<(\S*?)[^>]*>.*?</\ 1>|<.*?/>/

First and last white space characters: /^\s*|\s*$/

To find CSS properties: /^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}/

Extract page hyperlinks: /(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?',$follow_list).'))[^"rel="externalnofollow"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>/

Extract web picture: /\\<*[img][^\\\\>]*[src]*=*[\\"\\']{0,1}([^\\"\\'\\>]*)/

Xunlei link: /^thunder:\/\/[a-zA-Z0-9]+=$/

Ed2k link: /^ed2k:\/\/|file|.+|\/$/

Linux "file" path: /^\/(\w+\/)+\w+\. \w+$/

File path under window: /^[a-zA-Z]:\\(?:\w+\\)*\w+\. \w+$/

Life related

Amount (loose, can be negative, the first can be 0, and the decimal separation is supported): /^-? \d+(,\d{3})*(\.\d{1,2})?$/

Amount (greater than 0, two decimal places): /(^[1-9]{1}[0-9]*$)|(^[0-9]*\.[0-9]{2}$)/

Amount (strict, not negative, two digits after the decimal point at most, and the first digit is not 0): /(^[1-9]([0-9]+)? (\.[0-9]{1,2})?$)| (^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/

Passport: /(^[EeKkGgDdSsPpHh]\d{8}$)|(^(([Ee][a-fA-F])|([DdSsPp][Ee])|([Kk][Jj])|([Mm][Aa])|(1[45]))\d{7}$)/

Hong Kong Identity Card: /^[a-zA-Z]\d{6}\([\dA]\)$/

Macao ID Card: /^[1|5|7]\d{6}\(\d\)$/

Bay ID Card: /^[a-zA-Z][0-9]{9}$/

Stock code: /^(s[hz]|S[HZ])(000[\d]{3}|002[\d]{3}|300[\d]{3}|600[\d]{3}|60[\d]{4})$/


Department of this article@ Lao Li next door Network reprint to Yeluzi Blog , the article is for reference only, if you have any questions, please contact us.
comment (0)
 visitor
Top