Form validation Common (strongly recommended favorites) _ Regular expressions

Source: Internet
Author: User
Tags character classes html tags lowercase reserved

1. ^\d+$//matching nonnegative integer (positive integer + 0)
2. ^[0-9]*[1-9][0-9]*$//Matching positive integer
3. ^ ((-\d+) | (0+)) $//matching non positive integer (negative integer + 0)
4. ^-[0-9]*[1-9][0-9]*$//matching negative integers
5. ^-?\d+$//Matching integer
6. ^\d+ (\.\d+)? $//matching nonnegative floating-point number (positive floating-point number + 0)
7. ^ ([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $//matching positive floating-point numbers
8. ^ ((-\d+ (\.\d+)?) | (0+ (\.0+)) $//matching non-positive floating-point numbers (negative floating-point number + 0)
9. ^ (-([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $///Match negative floating-point number
10. ^ (-?\d+) (\.\d+)? $//matching floating-point number
11. ^[a-za-z]+$//Match a string of 26 English letters
12. ^[a-z]+$//Match a string of 26 uppercase letters
13. ^[a-z]+$//Match string consisting of 26 lowercase letters
14. ^[a-za-z0-9]+$//Match a string of numbers and 26 English letters
15. ^\w+$//Match A string of numbers, 26 English letters, or underscores
16. ^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$//matching email address
17. ^[a-za-z]+://Match (\w+ (-\w+) *) (\. ( \w+ (-\w+) *) * (\?\s*)? $//matching URL
18. Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]
19. Match Double-byte characters (including Chinese characters): [^\x00-\xff]
20. Application: Computes the length of the string (a double-byte character length meter 2,ascii character 1)
String.prototype.len=function () {return This.replace ([^\x00-\xff]/g, "AA"). Length;}
21st. A regular expression that matches a blank row: \n[\s|] *\r
22. Regular expression matching HTML tags:/< (. *) >.*<\/\1>|< (. *) \/>/
23. Matching a regular expression with a trailing space: (^\s*) | (\s*$)

* Regular expression Use cases
* 1, ^\s+[a-z a-z]$ can not be empty can not be a space can only be English letters
* 2, \s{6, can not be empty for more than six digits
* 3, ^\d+$ can not have a space can not be not a digital
* 4, (. *) (\.jpg|\.bmp) $ can only be JPG and BMP format
* 5, ^\d{4}\-\d{1,2}-\d{1,2}$ can only be 2004-10-22 format
* 6, ^0$ at least one of the selected
* 7, ^0{2,}$ select at least two
* 8, ^[\s|\s]{20,}$ can not be empty for more than 20 words
* 9, ^\+? [A-z0-9] (([-+.]| [_]+)? [a-z0-9]+) *@ ([A-z0-9]+ (\.| \-)) +[a-z]{2,6}$ Mail
* 10, \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * ([,;] \s*\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *) * Enter multiple addresses separate messages with commas or spaces
* 11, ^ ([0-9]+\)]? [0-9] {7,8}$ Phone number 7-bit or 8-bit or preceded by an area code such as (022) 87341628
* 12, ^[a-z-Z 0-9 _]+@[a-z-z 0-9 _]+ (\.[ A-Z 0-9 _]+) + (\,[a-z-Z 0-9 _]+@[a-z-z 0-9 _]+ (\.[ A-Z 0-9 _]+) + *$
* Can only be letters, numbers, underscores; must have @ and. Simultaneous format to standardize messages
* ^\w+@\w+ (\.\w+) + (\,\w+@\w+ (\.\w+) +) *$ The above expression can also be written in this way, more concise.
^\w+ ((-\w+) | ( \.\w+)) *\@\w+ (\.| -) *\.\w+$ [/size] \w+)

Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]
Commentary: Matching Chinese is really a headache, with this expression will be easy to do

Match Double-byte characters (including Chinese characters): [^\x00-\xff]
Commentary: can be used to compute the length of a string (a double-byte character length meter 2,ascii 1 characters)

A regular expression that matches a blank row: \n\s*\r
Commentary: can be used to delete blank lines

Regular expressions that match HTML tags: < (\s*?) [^>]*>.*?| <.*? />
Commentary: The online version is too bad, the above can only match the part of the complex nested tags still powerless

A regular expression that matches the end-end whitespace character: ^\s*|\s*$
Commentary: A useful expression that can be used to delete white-space characters (including spaces, tabs, page breaks, and so on) at the end of a line at the beginning

Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
Commentary: Form validation is useful

Regular expressions that match URL URLs: [a-za-z]+://[^\s]*
Commentary: Online circulation of the version of the function is very limited, which can meet the basic requirements

Match account number is legal (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$
Commentary: Form validation is useful

Match domestic phone number: \d{3}-\d{8}|\d{4}-\d{7}
Commentary: Match form such as 0511-4405222 or 021-87888822

Matching Tencent QQ Number: [1-9][0-9]{4,}
Commentary: Tencent QQ number starting from 10000

Match China ZIP Code: [1-9]\d{5} (?! \d)
Commentary: China postal code is 6 digits

Matching ID: \d{15}|\d{18}
Commentary: China's ID card is 15-or 18-digit

Matching IP address: \d+\.\d+\.\d+\.\d+
Commentary: Useful when extracting IP addresses

Match a specific number:
^[1-9]\d*$//Matching positive integer
^-[1-9]\d*$//matching negative integers
^-? [1-9]\d*$//matching integer
^[1-9]\d*|0$//matching nonnegative integer (positive integer + 0)
^-[1-9]\d*|0$//matching non positive integer (negative integer + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$//matching positive floating-point numbers
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $//matching negative floating-point number
^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $//matching floating-point number
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$//matching nonnegative floating-point number (positive floating-point number + 0)
^ (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0$//matching non-positive floating-point numbers (negative floating-point number + 0)
Commentary: useful when dealing with large amounts of data, pay attention to corrections when applied

Match a specific string:
^[a-za-z]+$//Match a string of 26 English letters
^[a-z]+$//Match a string of 26 uppercase letters
^[a-z]+$//Match string consisting of 26 lowercase letters
^[a-za-z0-9]+$//Match a string of numbers and 26 English letters
^\w+$//Match A string of numbers, 26 English letters, or underscores

Regular several basic concepts:

1. Greed: +,*,?, {m,n}, etc. default is greedy match, that is, as many matches as possible, also called the maximum match
If the following is added, it is converted to a non greedy match and requires a high version of support

2. Get: By default (X|y) is to get matching, many times just testing, not necessarily required to get the matching data, especially in the nested matching or large data to be used to match (?: X|y), so as to improve efficiency, optimize the program.

3. Consumption: The default is the consumption of matching, generally in the preliminary search is not consumption matching.
For example, 2003-2-8 will become 2003-02-08.
If use/-(\d)-/second match will start from 8, thereby replacing only the first 2, the error
If you use/-(\d) (? =-)/Then the second match starts with the second--no characters are consumed

4. Pre-check: JS is divided into forward check and negative pre-check
As the above (? =pattern) is forward lookup, match the find string at the beginning of any string matching pattern. and (?!) pattern) is a negative lookup that matches the search string at the beginning of any string that does not match the pattern. Negative pre-check is sometimes used in the expansion of [^], [^] just some characters, and?! Can make the entire string.
5. Callback: Generally used in the replacement, that is, based on the use of matching content to return unused replacement value, thereby simplifying the program, the need for a high version of support

6. Reference: \num a reference to the obtained Num.
For example, ' (.) \1\1 ' matches AAA type. '(.) (.) \2\1 ' Match ABBA type.

Regular expression reserved word
^ (carat)
. (period)
[(Left Bracket}
$ (dollar sign)
((left parenthesis)
) (right parenthesis)
| (pipe)
* (asterisk)
+ (plus symbol)
? (question mark)
{(left curly bracket, or left brace)
\ Backslash

Regular expression reserved word
^ (carat)
. (period)
[(Left Bracket}
$ (dollar sign)
((left parenthesis)
) (right parenthesis)
| (pipe)
* (asterisk)
+ (plus symbol)
? (question mark)
{(left curly bracket, or left brace)
\ backslash View plaincopy to Clipboardprint?
Constructs match the

Construct match view Plaincopy to Clipboardprint?
Character
X character X
\ backslash Character
\0n octal value of the character 0n (0 <= n <= 7)
\0nn octal value of the character 0nn (0 <= n <= 7)
\0mnn octal value of characters 0mnn 0mnn (0 <= m <= 3, 0 <= n <= 7)
\XHH hexadecimal-value character 0xhh
\uhhhh hexadecimal-value character 0xhhhh
\ t tab (' \u0009 ')
\ n line feed (' \u000a ')
\ r return character (' \u000d ')
\f page feed (' \u000c ')
\a Bell character (' \u0007 ')
\e Escape character (' \u001b ')
\CX t corresponds to X's control character X

Character
X character X
\ backslash Character
\0n octal value of the character 0n (0 <= n <= 7)
\0nn octal value of the character 0nn (0 <= n <= 7)
\0mnn octal value of characters 0mnn 0mnn (0 <= m <= 3, 0 <= n <= 7)
\XHH hexadecimal-value character 0xhh
\uhhhh hexadecimal-value character 0xhhhh
\ t tab (' \u0009 ')
\ n line feed (' \u000a ')
\ r return character (' \u000d ')
\f page feed (' \u000c ')
\a Bell character (' \u0007 ')
\e Escape character (' \u001b ')
\CX t corresponds to X's control character x view Plaincopy to Clipboardprint?
Character class
[ABC] A, B, or C (simple Class)
[^ABC] Any character other than a, B, or C (negation)
[A-za-z] A to Z or A to Z, inclusive (range)
[A-Z-[BC]] A to Z, except B and C: [Ad-z] (minus)
[A-z-[m-p]] A to Z, except m to P: [A-lq-z]
[A-z-[^def]] D, E, or F

Character class
[ABC] A, B, or C (simple Class)
[^ABC] Any character other than a, B, or C (negation)
[A-za-z] A to Z or A to Z, inclusive (range)
[A-Z-[BC]] A to Z, except B and C: [Ad-z] (minus)
[A-z-[m-p]] A to Z, except m to P: [A-lq-z]
[A-z-[^def]] D, E, or F view plaincopy to Clipboardprint?
Predefined character classes
. Any character (may be able to match the line terminator, maybe not)
\d number: [0-9]
\d Non-digit: [^0-9]
\s spaces: [\t\n\x0b\f\r]
\s Non spaces: [^\s]
\w Word characters: [a-za-z_0-9]
\w non-word characters: [^\w]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.