A detailed explanation of the application of the basic regular expression syntax (required ).

Source: Internet
Author: User

A detailed explanation of the application of the basic regular expression syntax (required ).

1. Basic regular expression syntax

Two special symbols '^' and '$ '. They indicate the start and end of a string respectively. Example:

"^ The": indicates all strings starting with "The" ("There", "The cat", etc );

"Of despair $": indicates the string ending with "of despair;

"^ Abc $": indicates that the start and end of the string are "abc"-Haha, only "abc" itself;

"Notice": indicates any string containing "notice.

As in the last example, if you do not use two special characters, you are indicating that the string to be searched is in any part of the searched string-you do not position it at a certain top.

Others include '*', '+', and '? 'These three symbols indicate the number of repeated occurrences of one or more characters. They indicate "none or more", "one or more", and "none or one ". The following are examples:

"AB *": indicates that a string has one a followed by zero or several B. ("A", "AB", "abbb ",......);

"AB +": indicates that a string is followed by at least one B or more;

"AB? ": Indicates that a string has one a followed by zero or one B;

"? B + $ ": indicates that there are zero or one a followed by one or several B at the end of the string.

You can also use a range enclosed in braces to indicate the range of repeated times.

"AB {2}": indicates that a string has a followed by two B ("abb ");

"AB {2,}": indicates that a string contains at least two B strings;

"AB {3, 5}": indicates that a string has 3 to 5 B following.

Note that you must specify the lower limit of the range (for example, "{0, 2}" instead of "{, 2 }"). Also, you may have noticed that '*', '+' and '? 'Is equivalent to "{0,}", "{1,}", and "{0, 1 }".

There is also a 'region', which indicates "or" Operation:

"Hi, hello": indicates that a string contains "hi" or "hello ";

"(B effeccd) ef": "bef" or "cdef ";

"(A between B) * c": represents a string of "a" "B" mixed strings followed by a "c ";

'.' Can replace any character:

"A. [0-9]": indicates that a string has a "a" followed by an arbitrary character and a number;

"^. {3 }$": represents a string of any three characters (Length: 3 characters );

Square brackets indicate that certain characters can appear at a specific position in a string:

"[AB]": indicates that a string has a "a" or "B" (equivalent to "a between B ");

"[A-d]": indicates that a string contains one of the lower-case 'A' to 'D' (equivalent to "a character B character c character d" or "[abcd]");

"^ [A-zA-Z]": a string that starts with a letter;

"[0-9] %": indicates a digit before the percent sign;

", [A-zA-Z0-9] $": represents a string ending with a comma followed by a letter or number.

You can also use '^' in square brackets to indicate unwanted characters. '^' should be the first character in square brackets. (For example, "% [^ a-zA-Z] %" indicates that no letter should appear in two percentage signs ).

In order to express it word by word, you must "^. $ () then * +? {\ "Are preceded by the transfer character '\'.

Note that escape characters are not required in square brackets.

2. Regular Expression verification control text box input character type

1. Only numbers and English letters can be entered:

<Input onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g, '')" ID = "Text1" NAME = "Text1">

2. Only numbers can be entered:

<Input onkeyup = "value = value. replace (/[^ \ d]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g, '')" ID = "Text2" NAME = "Text2">

3. Only the fullwidth fields can be entered:

<Input onkeyup = "value = value. replace (/[^ \ uFF00-\ uFFFF]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ uFF00-\ uFFFF]/g, '')" ID = "Text3" NAME = "Text3">

4. Only Chinese characters can be entered:

<Input onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ u4E00-\ u9FA5]/g, '')" ID = "Text4" NAME = "Text4">

3. General description of Regular Expressions

// Check whether it is composed of digits

/^ [0-9] {1, 20} $/

^ Indicates that the headers must match the rules followed by ^.

$ Indicates that the headers must match the rules that match the top $ character.

The content in [] is an optional character set.

[0-9] indicates that the character range is 0-9.

{} Indicates that the length of a numeric string is valid from 1 to 20, that is, the number of occurrences of characters in [0-9] ranges from 1 to 20.
/^ And $/are used in pairs to indicate that the entire string must fully match the defined rule, instead of matching only one substring in the string.

// Check Logon Name: You can enter only 5-20 strings starting with a letter, which can contain numbers, "_", and ".".

/^ [A-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {} $/

^ [A-zA-Z] {1} indicates that the first character must be a letter.

([A-zA-Z0-9] | [. _]) {} indicates a string with a length of 4 to 9 characters starting from the second digit (because it follows the last expression, it must contain uppercase/lowercase letters, numbers, or special character sets [..

// Check user name: only 1-30 strings starting with letters can be entered

/^ [A-zA-Z] {1, 30} $/

// Password verification: only 6-20 letters, numbers, and underscores can be entered

/^ (\ W) {6, 20} $/

\ W: Used to match letters, numbers, or underscores
 
// Check the common phone number and fax number. It can start with "+" or a number and can contain "-" and "".

/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $/

\ D: Used to match numbers from 0 to 9;

"?" Metacharacter specifies that the leading object must appear zero or once consecutively in the target object

Matching strings include: + 123-999 999; + 123-999 999; 123 999 999; + 123 999999

// Verify the URL

/^ Http [s] {0, 1 }:\/\/. + $/or/^ http [s] {0, 1 }:\/\/. {1, n }$/(the url string length is length ("https: //") + n)

\/: The character "/".

. Indicates the set of all characters

+ It is equivalent to {1,}, that is, 1 to infinity.

4. Regular Expression application (common part)

"^ \ D + $" // non-negative integer (positive integer + 0)

"^ [0-9] * [1-9] [0-9] * $" // positive integer

"^ (-\ D +) | (0 +) $" // non-positive integer (negative integer + 0)

"^-[0-9] * [1-9] [0-9] * $" // negative integer

"^ -? \ D + $ "// integer

"^ \ D + (\. \ d + )? $ "// Non-negative floating point number (Positive floating point number + 0)

"^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// Positive floating point number

"^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $ "// Non-Positive floating point number (negative floating point number + 0)

"^ (-([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

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

"^ [A-Za-z] + $" // A string consisting of 26 English letters

"^ [A-Z] + $" // a string consisting of 26 uppercase letters

"^ [A-z] + $" // a string consisting of 26 lowercase letters

"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters

"^ \ W + $" // a string consisting of digits, 26 letters, or underscores

"^ [\ W-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ "// email address

"^ [A-zA-z] +: // (\ w + (-\ w + )*)(\. (\ w + (-\ w + )*))*(\? \ S *)? $ "// Url

/^ (D {2} | d {4})-(0 ([1-9] {1}) | (1 [1 | 2]) -([0-2] ([1-9] {1}) | (3 [0 | 1]) $ // year-month-day

/^ (0 ([1-9] {1}) | (1 [1 | 2]) /([0-2] ([1-9] {1}) | (3 [0 | 1]) /(d {2} | d {4}) $ // month/day/year

"^ ([W-.] +) @ ([0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }.) | ([w-] + .) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (]?) $ "// Emil

"(D + -)? (D {4 }-? D {7} | d {3 }-? D {8} | ^ d {7, 8}) (-d + )? "// Phone number

"^ (D {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1, 2} | 1dd | 2 [0-4] d | 25 [0-5]) $ "// ip address

^ ([0-9A-F] {2}) (-[0-9A-F] {2}) {5} $ // Regular Expression of the MAC address

^ [-+]? \ D + (\. \ d + )? $ // Value Type Regular Expression

The above detailed explanation of the application of the basic syntax of Regular Expressions (which must be read) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support the regular expression.

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.