1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| * 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}$ * Telephone number (“XXX-XXXXXXX”, ”XXXX-XXXXXXXX”, “XXX-XXXXXXX”, “XXX-XXXXXXXX”, “XXXXXXX” and “XXXXXXXX”): ^($$d{3,4}-)|d{3.4}-)?d{7,8}$ * Domestic telephone number: d{3}-d{8}|d{4}-d{7} * ID card number (15 or 18 digits): ^d{15}|d{18}$ * Short ID number (digits, ending with the letter x): ^([0-9]){7,18}(x|X)?$ or ^d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$ * Whether an account is valid (starts with a letter, 5-16 bytes allowed, letters, digits, and underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ * Password (starts with a letter, length between 6 and 18, may only contain letters, digits, and underscores): ^[a-zA-Z]w{5,17}$ * Strong password (must contain a combination of upper- and lowercase letters and digits, may not use special characters, length between 8 and 10): ^(?=._d)(?=._[a-z])(?=.*[A-Z]).{8,10}$ * Date format: ^d{4}-d{1,2}-d{1,2} * The 12 months of a year (01-09 and 1-12): ^(0?[1-9]|1[0-2])$ * The 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 money representations we can accept: “10000.00” and “10,000.00”, and “10000” and “10,000” without the “cents”: ^[1-9][0-9]*$ * This represents any number not starting with 0, but it also means a single “0” will not pass, so we use the following form: ^(0|[1-9][0-9]*)$
* A 0, or a number not starting with 0. We can also allow a leading minus sign: ^(0|-?[1-9][0-9]*)$ * This represents a 0, or a number that may be negative and does not start with 0. Let the user start with 0. Then drop the minus sign too, since money can never be negative. What we add next is the optional decimal part: ^[0-9]+(.[0-9]+)?$ * It must be said that there should be at least 1 digit after the decimal point, so “10.” will not pass, but “10” and “10.2” do pass: ^[0-9]+(.[0-9]{2})?$ * This way we require exactly two digits after the decimal point; if you think that is too strict, you can do this: ^[0-9]+(.[0-9]{1,2})?$ * This allows the user to write just one decimal place. Next we should consider the comma in numbers, and we can do this: ^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$ * 1 to 3 digits, followed by any number of comma+3 digits; the comma becomes optional rather than required: ^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$ * Note: this is the final result. Do not forget that “+” can be replaced by “*”. And if you think an empty string is acceptable too (strange, why?) — finally, do not forget to strip that backslash when using the function; that is where the usual mistakes are * xml file: ^([a-zA-Z]+-?)+[a-zA-Z0-9]+.[x|X][m|M][l|L]$ * Regular expression for Chinese characters: [一-龥] * Double-byte characters: [^-ÿ] (including Chinese characters; it can be used to calculate the length of a string (a double-byte character counts as 2, an ASCII character counts as 1)) * Regular expression for blank lines: s* (can be used to delete blank lines) * Regular expression for HTML tags: ``<(S_?)[^>]_>._?</>|<._? />`` (the versions circulating online are terrible; the one above can only handle part of it, and it is still powerless against complex nested tags) * Regular expression for leading and trailing whitespace: ^s_|s_$ or (^s_|)|(s_$) (can be used to delete whitespace at the start and end of lines (including spaces, tabs, form feeds, and so on), a very useful expression) * Tencent QQ number: [1-9][0-9]{4,} (Tencent QQ numbers start at 10000) * Chinese postal code: [1-9]d{5}(?!d) (Chinese postal codes are 6 digits) * IP address: d+.d+.d+.d+ (useful when extracting IP addresses) * 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 @飞龙三少, thanks for sharing)
|