Regular expression

xiaoxiao2021-03-06  76

1. Regular expression basic syntax

Two special symbols '^' and '$'. Their role is to point out the beginning and end of a string. The example is as follows:

"^ The": indicates all strings starting with "THE" ("there", "the cat", etc.); "Of despair $": means the string ending with "of despair"; "^ ABC $" : Indicates that the start and end are "ABC" strings - huh, only "ABC"; "notice": indicates any string containing "notice".

The last example, if you don't use two special characters, you are in any part of the string to find the string - you don't position it on a top.

Others have the three symbols of '*', ' ' and '' ', indicating that one or a sequence character is repeated. They represent "no or more", "once or more" and "no or once". Here are a few examples:

"ab *": means a string having a A back followed by zero or several b. ("A", "ab", "abbb", ...); "ab ": indicates a string having a A followed by at least one B or more; "ab?": Indicates a string has a A back Follow zero or one B; "a? B $": indicates that there is zero or one A followed one or more b at the end of the string.

You can also use the scope, enclose with braces to indicate the range of repetitions.

"AB {2}": means a string has a A follow 2 B ("abb"); "AB {2,}": indicates a string having a A follow at least 2 b; "AB {3, 5} ": Represents a string having a A follows 3 to 5 B.

Note that you must specify the lower limit of the range (such as "{0, 2}" instead of "{, 2}"). Also, you may notice, '*', ' ' and '' '' correspond to "{0,}", "{1,}" and "{0, 1}". There is also a '|', indicating "or" operation:

"hi|hello": means "hi" or "hello"; "indicates" bef "or" cdef ";" (AQ) * C ": indicating" bef "or" cdef ";" The string "a" "b" mixed string followed by "C";

'.' Can replace any character:

"a. [0-9]": indicates a string with a "A" followed by an arbitrary character and a number; "^. {3} $": indicates a string with any three characters (length 3 Character);

Square brackets indicate that certain characters have occurred in a particular location in a string: "[ab]": means a string has a "A" or "B" (equivalent to "A|B"); "[[[[ AD]: Represents a string contains one of 'a' into 'd' (equivalent to "A|B|C|D" or "[ABCD]"); "^ [A-ZA-Z] ": Represents a string that starts with letters;" [0-9]% ": indicates a number in front of a percentage;", [A-ZA-Z0-9] $ ": Represents a string A comma is followed by a letter or numbers.

You can also use '^' in square brackets to indicate the characters that do not want to appear, '^' should in square brackets. (, Such as "% [^ a-za-z]%" means that letters should not appear in two percent sign).

In order to express verbatics, you must add transfer characters '/' before "^. $ () | * ? {/".

Please note that in square brackets, no escape characters need. 2. Regular expression verify the input character type of the control text box 1. You can only enter numbers and English: 2. Can only enter numbers : 3. You can only enter a full range of: 4. You can only enter Chinese characters: 3. Application instance of regular expression Popular statement *************************************************************** ******************************* / / The verification is all composed of numbers / ^ [0-9] {1, 20} $ / ^ indicates that the character should match the rule of the next rule $ indicating that the character should be matched to match the content in the previous rules [0-9] indicates that the character range is required 0-9 Between {1, 20} indicates that the number string length is legally 1 to 20, that is, the number of characters in [0-9] is 1 to 20 times.

/ ^ And $ / to use should be a rule that requires the entire string full match definition, not only one of the strings.

*********************************************************** **************************** // Check Login Name: You can only enter 5-20 letters, with numbers , "_", "." The string / ^ [-za-z] }([a-za-z0-9]|[._]) {4, 19}?

^ [A-ZA-Z] {1} indicates that the first character requires letters. ([A-ZA-Z0-9] | [._]) {4,19} Indicates a string from the second bit (because it is followed by the last expression) string of 4 to 9 bits It is required to consist of cases, numbers, or special character sets [._]. *********************************************************** ************************************* // Check users name: You can only enter 1-30 strings starting with letters / ^ [A-ZA-Z] {1,30} $ /

*********************************************************** ***************************** // Check password: only 6-20 letters, numbers, underline / ^ ( / w) {6, 20} $ /

/ W: Used to match letters, numbers or underscore characters

*********************************************************** **************************** // verify ordinary call, fax number: " " or digital start, can contain " - "and" / ^ [ ] {0,1} (/ d) {1,3} []? ([-]? (/ D) | []) {1,12}) $ /

/ d: Used to match the number from 0 to 9; "?" Metacity specifies that its leading object must have occurred continuous or once in the target object.

The string can be matched, such as: 123 -999 999; 123-999 999; 123 999 999; 123 999999, etc.

*********************************************************** ***************************** / / Check URL /^http[s]]] 0 0 / / / [ Or /^ http[s]{ 0} ("https: //") n) ("https: //") n)

/ /: Indicates characters "/". It means that all the characters are equivalent to {1,}, which is 1 to be infinite.

*********************************************************** ***************************** // verification Chinese characters / ^ [/ u4e00- / u9fa5] $ /

[/ u4e00- / u9fa5]: Estimate is the range of Chinese character sets

The above expression is tested in the JavaScript below