Commonly used regular expressions
- regex101 (Regular Expression Test Tool)
- REGEXP (regular expression testing tool)
- Regulex (regular expression visualization tool)
Thanks to any-rule project
Thanks to learn-regex project
🍔 Regularity
train train number
/^[GCDZTSPKXLY1-9]\d{1,4}$/
Mobile phone body code (IMEI)
/^\d{15,17}$/
URL (or ip) that must have a port number
/^((ht|f)tps?:\/\/)? [\w-]+(\. [\w-]+)+:\d{1,5}\/? $/
URL (URL)
/^(((ht|f)tps?):\/\/)? ([^! @#$%^&*?. \s-]([^! @#$%^&*? \s]{0,63}[^! @#$%^&*? \s])? \.) +[a-z]{2,6}\/? /
Uniform Social Credit Code
/^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/
Uniform social credit code (loose match) (15 digits/18 digits/20 digits/letters)
/^(([0-9A-Za-z]{15})|([0-9A-Za-z]{18})|([0-9A-Za-z]{20}))$/
Thunderbolt links
/^thunderx?:\/\/[a-zA-Z\d]+=$/
ed2k links (loose match)
/^ed2k:\/\/\\|file\|. +\|\/$/
magnetic links (loose match)
/^magnet:\?xt=urn:btih:[0-9a-fA-F]{40,}. *$/
Subnet mask (not including 0.0.0.0)
/^(254|252|248|240|224|192|128)\.0\.0\.0\.0|255\. (254|252|248|240|224|192|128|0)\.0\.0|255\.255\. (254|252|248|240|224|192|128|0)\.0|255\.255\.255\. (255|254|252|248|240|224|192|128|0)$/
ðŸ™' ðŸ™'
### linux "hidden files" path
```javascript
/^\/(? :[^/]+\/)*\. [^/]*/
linux folder path
/^\/(? :[^/]+\/)*$/
linux file paths
/^\/(? :[^/]+\/)*[^/]+$/
window "folder" path
/^[a-zA-Z]:\\(? :\w+\\\\?) *$/
path to "file" under window
/^[a-zA-Z]:\\\(? :\w+\\\)*\w+\. \w+$/
Stock code (A share)
/^(s[hz]|S[hz])(000[\d]{3}|002[\d]{3}|300[\d]{3}|600[\d]{3}|60[\d]{4})$/
greater than or equal to 0, less than or equal to 150, supports decimal places of 5, e.g. 145.5, for determining exam paper marks
/^150$|^(? :\d|[1-9]\d|1[0-4]\d)(? :\.5)? $/
html comments
/<! --[\s\S]*? -->/g
md5 format (32-bit)
/^([a-f\d]{32}|[A-F\d]{32})$/
GUID/UUID
/^[a-f\d]{4}(? :[a-f\d]{4}-){4}[a-f\d]{12}$/i
The version number (version) format must be X.Y.Z
/^\d+(? :\. \d+){2}$/
Video (video) link address (video format can be added or removed as required)
/^https?:\/\/(. +\/)+. +(\. (swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4))$/i
Image (image) link address (image format can be added or removed as required)
/^https?:\/\/(. +\/)+. +(\. (gif|png|jpg|jpeg|webp|svg|psd|bmp|tif))$/i
24-hour time (HH:mm:ss)
/^(? :[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/
12-hour time (hh:mm:ss)
/^(? :1[0-2]|0?[1-9]):[0-5]\d:[0-5]\d$/
base64 format
/^\s*data:(? :[a-z]+\/[a-z0-9-+.] +(? :;[a-z-]+=[a-z0-9-]+)? ? (? :;base64)? ,([a-z0-9!$&',()*+;=\-. _~:@/? %\s]*?) \s*$/i
Numeric/currency amounts (supports negative numbers, thousand separators)
/^-? \d+(,\d{3})*(\. \d{1,2})? $/
Numeric/currency amounts (positive numbers only, no checksum thousand separators)
/(? :^[1-9]([0-9]+)? (? :\. [0-9]{1,2})? $)|(? :^(? :0)$)|(? :^[0-9]\. [0-9](? :[0-9])? $)/
Bank card number (10 to 30 digits, covering public/private accounts, refer to WeChat Pay)
/^[1-9]\d{9,29}$/
Chinese Name
/^(? :[\u4e00-\u9fa5-]{2,16})$/
English Name
/(^[a-zA-Z][a-zA-Z\s]{0,20}[a-zA-Z]$)/
License plate number (new energy)
/^[京津沪渝冀豫雲辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽Guizhou青藏川宁琼使领][A-HJ-NP-Z](? :((\d{5}[A-HJK])|([A-HJK][A-HJ-NP-Z0-9][0-9]{4}))|[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9 Hanging School Police Hong Kong and Macau])$/
Vehicle registration number (non-new energy)
/^[京津沪渝冀豫雲辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]$/
Vehicle registration number (new energy + non-new energy)
/^[京津沪渝冀豫雲辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/
mobile phone (mobile phone) China (rigorous), according to the Ministry of Industry and Information Technology 2019 the latest announced mobile phone number segment
/^(? :(? :\+|00)86)?1(? :(? :3[\d])|(? :4[5-79])|(? :5[0-35-9])|(? :6[5-7])|(? :7[0-8])|(? :8[\d])|(? :9[189]))\d{8}$/
mobile phone China (loose), as long as it starts with 13,14,15,16,17,18,19
/^(? :(? :\+|00)86)?1[3-9]\d{9}$/
mobile phone China (most lenient), as long as it starts with 1, if your phone number is used to receive SMS, this one is preferred
/^(? :(? :\+|00)86)?1\d{10}$/
date(date)
/^\d{1,4}(-)(1[0-2]|0?[1-9])\1(0?[1-9]|[1-2]\d|30|31)$/
Time that can be successfully transformed by moment YYYYMMDD HH: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$/
email(mailbox)
/^(([^<>()[\]\\. ,;:\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,}))$/
tel phone (domestic), e.g.: 0341-86091234
javascript /^(? :(? :\d{3}-)? \d{8}|^(? :\d{4}-)? \d{7,8})(? :-\d+)? $/
### ID number (1 generation, 15 digits)
```javascript
/^[1-9]\d{7}(? :0\d|10|11|12)(? :0[1-9]|[1-2][\d]|30|31)\d{3}$/
ID number (2 generations, 18 digits), last digit is a check digit, may be a number or character X
/^[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]$/
ID number, support 1/2 generation (15 digits/18 digits)
/^\d{6}((((((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})02(0[1-9]|1\d|2[0-8] )|((((19|20)([13579][26]|[2468][048]|0[48])))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1 [02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/
Passport (including Hong Kong and Macau)
/(^[EeKkGgDdSsPpHh]\d{8}$)|(^(([Ee][a-fA-F])|([DdSsPp][Ee])|([Kk][Jj])|([Mm][Aa])|(1[45]))\d{7}$)/
Account number is legal (starts with a letter, allows 5-16 bytes, allows alphanumeric underscore combinations)
/^[a-zA-Z]\w{4,15}$/
Chinese/Chinese characters
/^(? :[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F -\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\ \uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/
Decimals
/^\d+\. \d+$/
Numbers
/^\d{1,}$/
html tags (loose match)
/<(\w+)[^>]*>(. *? <\/\1>)? /
The qq number is in the correct format
/^[1-9][0-9]{4,10}$/
Number and letter composition
/^[A-Za-z0-9]+$/
English letters
/^[a-zA-Z]+$/
Lowercase alphabetic composition
/^[a-z]+$/
Uppercase alphabet
/^[A-Z]+$/
Password strength checksum, minimum 6 digits, including at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character
/^\S*(? =\S{6,})(? =\S*\d)(? =\S*[A-Z])(? =\S*[a-z])(? =\S*[! @#$%^&*? ])\S*$/
Username checksum, 4 to 16 digits (letters, numbers, underscores, minus signs)
/^[a-zA-Z0-9_-]{4,16}$/
ip-v4[:port]
/^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.) {3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(? ::(? :[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))? $/
ip-v6[:port]
/(^(? :(? :(? :[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(((0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(((0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)|((0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)? [0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0, 3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2 })|(2[0-4]\d)|(\d{1,2}))\b)\.) {3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)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0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1 ,4}:){0,6}[0-9A-Fa-f]{1,4})|(([[0-9A-Fa-f]{1,4}:){1,7}:))\](? ::(? :[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))? $)/i
Hexadecimal colours
/^#? ([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
Micro signal (wx), 6 to 20 digits, starts with a letter, alpha, numeric, minus, underscore
/^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/
Postcode (China)
/^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/
Chinese and numbers
/^((? :[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F -\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\ \uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])|(\d))+$/
Cannot contain letters
/^[^A-Za-z]*$/
java package name
/^([a-zA-Z_]\w*)+([.] [a-zA-Z_]\w*)+$/
mac address
/^((([[a-f0-9]{2}:){5})|(([a-f0-9]{2}-){5}))[a-f0-9]{2}$/i
Match consecutive repeated characters
/(.) \1+/
A number and a letter of the alphabet, and containing both a number and a letter of the alphabet
/^(? =. *[a-zA-Z])(? =. *\d). +$/
Hong Kong Identity Cards
/^[a-zA-Z]\d{6}\([\dA]\)$/
Macao Identity Card
/^[1|5|7]\d{6}\(\dA]\)$/
Taiwan Identity Card
/^[a-zA-Z][0-9]{9}$/
Uppercase letters, lowercase letters, numbers, special symbols @#$%^&*
~()-+=` in any 3 passwords
/^(?! [a-zA-Z]+$)(?! [A-Z0-9]+$)(?! [A-Z\W_! @#$%^&*`~()-+=]+$)(?! [a-z0-9]+$)(?! [a-z\W_! @#$%^&*`~()-+=]+$)(?! [0-9\W_! @#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_! @#$%^&*`~()-+=]/
Positive integers, not including 0
/^\+? [1-9]\d*$/
Negative integers, not containing 0
/^-[1-9]\d*$/
Integers
/^-? [0-9]\d*$/
Floating point numbers
/^(-? \d+)(\. \d+)? $/
email (Chinese email supported)
/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\. [a-zA-Z0-9_-]+)+$/