The most complete collection of commonly used regular expressions
Many friends who don’t know much about the rules often search online for a long time when they need to use the rules to verify the data, but the results still do not meet the requirements. So I recently sorted out some regular expressions commonly used in development, including verification of numbers, characters, some special needs, etc. Leave a brief for yourself and provide reference for your friends.
1. Verify the expression of numbers
Number: ^[0-9]*$
n-digit number: ^\d{n}$
Numbers with at least n digits: ^\d{n,}$
Number of m-n bits: ^\d{m,n}$
Numbers starting with zero and non-zero: ^(0|[1-9][0-9]*)$
Numbers with a maximum of two decimal places starting with non-zero: ^([1-9][0-9]*)+(.[0-9]{1,2})?$
Positive or negative numbers with 1-2 decimal places: ^(\-)?\d+(\.\d{1,2})?$
Positive, negative, and decimal: ^(\-|\+)?\d+(\.\d+)?$
Positive real numbers with two decimals: ^[0-9]+(.[0-9]{2})?$
Positive real numbers with 1 to 3 decimal places: ^[0-9]+(.[0-9]{1,3})?$
Nonzero positive integer: ^[1-9]\d*$ or ^([1-9][0-9]*){1,3}$ or ^\+?[1-9][0-9]*$
Negative integers with nonzero: ^\-[1-9][]0-9″*$ or ^-[1-9]\d*$
Non-negative integer: ^\d+$ or ^[1-9]\d*|0$
Non-positive integer: ^-[1-9]\d*|0$ or ^((-\d+)|(0+))$
Non-negative floating point number: ^\d+(\.\d+)?$ or ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
Non-positive floating point number: ^((-\d+(\.\d+)?)|(0+(\.0+)?))$ or ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
Positive floating point number: ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ or ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
Negative floating point number: ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ or ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
Floating point number: ^(-?\d+)(\.\d+)?$ or ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
2. Verify the expression of character
Chinese characters: ^[\u4e00-\u9fa5]{0,}$
English and numbers: ^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$
All characters with lengths of 3-20: ^.{3,20}$
A string composed of 26 English letters: ^[A-Za-z]+$
A string consisting of 26 capital English letters: ^[A-Z]+$
A string consisting of 26 lowercase English letters: ^[a-z]+$
String consisting of numbers and 26 English letters: ^[A-Za-z0-9]+$
String consisting of numbers, 26 English letters or underscores: ^\w+$ or ^\w{3,20}$
Chinese, English, and numbers include underscores: ^[\u4E00-\u9FA5A-Za-z0-9_]+$
Chinese, English, numbers but not underscores and other symbols: ^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
You can enter characters such as ^%&',;=?$\": [^%&',;=?$\x22]+
The input of characters containing ~ is prohibited: [^~\x22]+
3. Special requirements expression
Email address: ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
Domain name: [a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
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}$
Phone number ("XXX-XXXXXXXXX", "XXXX-XXXXXXXXX", "XXX-XXXXXXXXX", "XXXXXXXXXXXXX, "XXXXXXXXXXXXXXXX): ^($$\d{3,4}-)|\d{3.4}-)?\d{7,8}$
Domestic phone number (0511-4405222, 021-87888822): \d{3}-\d{8}|\d{4}-\d{7}
ID number (15 digits, 18 digits): ^\d{15}|\d{18}$
Short ID number (number, letter ending x): ^([0-9]){7,18}(x|X)?$ or ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
Is the account legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Password (starting with letters, lengths between 6 and 18, can only contain letters, numbers and underscores): ^[a-zA-Z]\w{5,17}$
Strong password (must contain a combination of upper and lower case letters and numbers, special characters cannot be used, the length is between 8-10): ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
Date format: ^\d{4}-\d{1,2}-\d{1,2}
12 months of a year (01-09 and 1-12): ^(0?[1-9]|1[0-2])$
31 days of a month (01-09 and 1-31): ^((0?[1-9])|((1|2)[0-9])|30|31)$
Money input format:
- There are four forms of money that we can accept: "10000.00" and "10,000.00", and "10,000" without "score":^[1-9][0-9]*$
- This means any number that does not start with 0, but it also means that a character "0" does not pass, so we take the following form: ^(0|[1-9][0-9]*)$
- A 0 or a number that does not start with 0. We can also allow a minus sign to start with: ^(0|-?[1-9][0-9]*)$
- This means a 0 or a number that may be negative and not 0. Let the user start with 0. Remove the negative sign, because money cannot be negative. What we want to add below is the decimal part that explains the possible: ^[0-9]+(.[0-9]+)? $
- It must be noted that there should be at least one digit after the decimal point, so "10." is not passed, but "10" and "10.2" are passed: ^[0-9]+(.[0-9]{2})?$
- In this way, we stipulate that there must be two digits after the decimal point. If you think it is too harsh, you can do this: ^[0-9]+(.[0-9]{1,2})?$
- This allows the user to write only one decimal. Now we should consider the commas in the number, we can do this: ^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$
- 1 to 3 numbers, followed by any comma + 3 numbers, the comma becomes optional, instead of having to: ^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$
- Note: This is the final result, don’t forget that “+” can be replaced by “*”. If you think empty strings are acceptable (strange, why?) Finally, don't forget to remove the backslash when using functions. The general errors are here
xml file: ^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$
Regular expression of Chinese characters: [\u4e00-\u9fa5]
Double-byte characters: [^\x00-\xff] (including Chinese characters, it can be used to calculate the length of a string (one double-byte character length meter 2, ASCII character meter 1))
Regular expression for blank lines: \n\s*\r (can be used to delete blank lines)
Regular expressions for HTML tags: <(\S*?)[^>]*>.*?</\1>|<.*? /> (The version circulated on the Internet is too bad, and the above is only partial, and I still can't do anything about complex nested tags)
Regular expressions of the beginning and end whitespace characters: ^\s*|\s*$ or (^\s*)|(\s*$) (can be used to delete whitespace characters at the end of the line (including spaces, tabs, page breaks, etc.), a very useful expression)
Tencent QQ number: [1-9][0-9]{4,} (Tencent QQ number starts at 10000)
China Postal Code: [1-9]\d{5}(?!\d) (China Postal Code is 6 digits)
IP address: \d+\.\d+\.\d+\.\d+ (useful when extracting 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 @Tianzhang3, thanks for sharing)
The above are the most comprehensive regular expressions. I hope they will be helpful to everyone's learning. Please bookmark them quickly.
Related Articles
Use of regular expressions ASP
Regular expression usage ASP...2006-10-10See a JS regular question
I saw a JS regular question...2007-01-01JS regular expression position matching
Position matching code of regular expressions in JS, articles translated abroad.2009-12-12A brief analysis of regular expressions in js
This article mainly introduces an in-depth analysis of regular expressions in js. Friends who need it can refer to it.2017-06-06Regular expression basic tutorial
Regular expression basic tutorial regular expression...2007-03-03Description and application of regular expression functions in JScript
Description and application of regular expression functions in JScript...2007-04-04Methods for finding similar words in regular expressions
This article mainly introduces in detail the method of finding similar words in regular expressions, which has certain reference value. Interested friends can refer to it.2016-11-11Methods to remove empty lines through regular expressions
Our goal is to delete empty lines in text, maybe one line or multiple lines2014-05-05ExtJS4's textfield (textField) method to use regular expressions to verify (Regex)
Extjs' Regex verification properties are divided into two situations. One is that there is only a red prompt box; the other is that a prompt message similar to annotations will appear next to the prompt box, but when the page is loaded, you need to add (); prompt box to be displayed normally.2016-02-02Regular expression implementation and or non-relationship [Recommended]
This article mainly introduces the implementation and non-relationship of regular expressions. This article introduces you very detailedly and has certain reference value. Friends who need it can refer to it.2018-07-07