Regular expression syntax and validation

Source: Internet
Author: User
Tags alphabetic character

First, introduce the next few common simple symbols:

/love/the part where the "/" delimiter is located is the pattern that will be matched in the target object. The user simply puts the pattern content that you want to find the matching object in between the "/" delimiter. To enable the user to customize the schema content more flexibly, the regular expression provides a special "meta-character". A meta-character is a special character in a regular expression that can be used to specify its leading character (that is, the character in front of the metacharacters) in the target object.
The more commonly used metacharacters are: "+", "*", and "?".
The "+" metacharacters stipulate that their leading characters must appear one or more times in the target object.
The "*" meta-character specifies that its leading character must appear 0 or more times in the target object.
“?” A meta-character specifies that its leading object must appear 0 or more times in the target object.

Below, let's look at the specific application of the regular expression meta-character.
/fo+/because the preceding regular expression contains the "+" metacharacters, it can be matched with a string of "fool", "fo", or "football" in the target object, and so on with one or more letters O consecutively after the letter F.
/eg*/because the preceding regular expression contains the "*" metacharacters, it can be matched with a string of "easy", "ego", or "egg" in the target object, such as 0 or more consecutive letters G after the letter E.
/wil?/because the "?" is included in the preceding regular expression Metacharacters, which indicates that a string that matches the "Win" in the target object, or "Wilson", and so on after the letter I, appears 0 or one letter L.

Sometimes you don't know how many characters to match. To be able to adapt to this uncertainty, the regular expression supports the concept of qualifiers. These qualifiers can specify how many times a given component of a regular expression must appear to satisfy a match.
{n} n is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.
{N,} n is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ', but can match all o in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
{n,m} m and n are non-negative integers, where n <= m. Matches at least n times and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' O? '. Note that there can be no spaces between a comma and two numbers.

Specific symbol tables for regular expressions:

character Description
\ Marks the next character as a special character, or a literal character, or a backward reference, or an octal escape. For example, "n" matches the character "n". "\ n" matches a line break. The sequence "\ \" matches "\" and "\ (" Matches "(".
^ Matches the starting position of the input string. If the multiline property of the RegExp object is set, ^ also matches the position after "\ n" or "\ r".
$ Matches the end position of the input string. If the multiline property of the RegExp object is set, $ also matches the position before "\ n" or "\ r".
* Matches the preceding subexpression 0 or more times. For example, zo* can match "z" and "Zoo". * Equivalent to {0,}.
+ Matches the preceding subexpression one or more times. For example, "zo+" can Match "Zo" and "Zoo", but not "Z". + equivalent to {1,}.
? Matches the preceding subexpression 0 or one time. For example, "Do (es)?" You can match "do" in "do" or "does".?
N N is a non-negative integer. Matches the determined n times. For example, "o{2}" cannot match "O" in "Bob", but can match two o in "food".
{N,} N is a non-negative integer. Match at least n times. For example, "o{2,}" cannot match "O" in "Bob", but can match all o in "Foooood". "O{1,}" is equivalent to "o+". "O{0,}" is equivalent to "o*".
{N,m} Both M and n are non-negative integers, where n<=m. Matches at least n times and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". "o{0,1}" is equivalent to "O?". Note that there can be no spaces between a comma and two numbers.
? When the character immediately follows any other restriction (*,+,?,{n},{n,},{n,m}), the matching pattern is non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "Oooo", "o+?" A single "O" will be matched, and "o+" will match all "O".
. Matches any single character except "\ n". To match any character that includes "\ n", use a pattern like "[. \ n]".
(pattern) Match pattern and get this match. The obtained matches can be obtained from the resulting matches collection, the Submatches collection is used in VBScript, and the $0...$9 property is used in JScript. To match the parentheses character, use "\ (" or "\").
(?:p Attern) Matches pattern but does not get a matching result, which means that this is a non-fetch match and is not stored for later use. This is used in the or character "(|)" It is useful to combine the various parts of a pattern. For example, "Industr (?: y|ies)" is a more abbreviated expression than "industry|industries".
(? =pattern) Forward-checking matches the lookup string at the beginning of any string that matches the pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, "Windows (? =95|98| nt|2000) "Can match" windows "in" Windows2000 ", but does not match" windows "in" Windows3.1 ". Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.
(?! Pattern A negative pre-check matches the lookup string at the beginning of any string that does not match the pattern. This is a non-fetch match, which means that the match does not need to be acquired for later use. For example, "Windows (?! 95|98| nt|2000) "Can match" windows "in" Windows3.1 ", but does not match" windows "in" Windows2000 ". Pre-check does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check
X|y Match x or Y. For example, "Z|food" can match "Z" or "food". "(z|f) Ood" matches "Zood" or "food".
[XYZ] The character set is combined. Matches any one of the characters contained. For example, "[ABC]" can Match "a" in "plain".
[^XYZ] Negative character set. Matches any character that is not contained. For example, "[^ABC]" can match "P" in "plain".
[A-z] The character range. Matches any character within the specified range. For example, "[A-z]" can match any lowercase alphabetic character in the range "a" to "Z".
[^a-z] A negative character range. Matches any character that is not in the specified range. For example, "[^a-z]" can match any character that is not in the range "a" to "Z".
\b Matches a word boundary, which is the position between a word and a space. For example, "er\b" can Match "er" in "never", but cannot match "er" in "verb".
\b Matches a non-word boundary. "er\b" can Match "er" in "verb", but cannot match "er" in "Never".
\cx Matches the control character indicated by X. For example, \cm matches a control-m or carriage return. The value of x must be one of a-Z or a-Z. Otherwise, c is considered to be a literal "C" character.
\d Matches a numeric character. equivalent to [0-9].
\d Matches a non-numeric character. equivalent to [^0-9].
\f Matches a page break. Equivalent to \x0c and \CL.
\ n Matches a line break. Equivalent to \x0a and \CJ.
\ r Matches a carriage return character. Equivalent to \x0d and \cm.
\s Matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s Matches any non-whitespace character. equivalent to [^\f\n\r\t\v].
\ t Matches a tab character. Equivalent to \x09 and \ci.
\v Matches a vertical tab. Equivalent to \x0b and \ck.
\w Matches any word character that includes an underscore. Equivalent to "[a-za-z0-9_]".
\w Matches any non-word character. Equivalent to "[^a-za-z0-9_]".
\xn Match N, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long for a determination. For example, "\x41" matches "A". "\x041" is equivalent to "\x04&1". ASCII encoding can be used in regular expressions:
\num Matches num, where num is a positive integer. A reference to the obtained match. For example, "(.) \1 "matches two consecutive identical characters.
\ n Identifies an octal escape value or a backward reference. n is a backward reference if \ n is preceded by at least one of the sub-expressions obtained. Otherwise, if n is the octal number (0-7), N is an octal escape value.
\nm Identifies an octal escape value or a backward reference. If at least NM has obtained a subexpression before \nm, then NM is a backward reference. If there are at least N fetches before \nm, then n is a backward reference followed by the literal m. If none of the preceding conditions are met, if both N and M are octal digits (0-7), then \nm will match the octal escape value nm.
\nml If n is an octal number (0-3) and both M and L are octal digits (0-7), the octal escape value NML is matched.
\un Match N, where N is a Unicode character represented by four hexadecimal digits. For example, \u00a9 matches the copyright symbol (?).
There are two ways to use it:

Method One:
var re=/regular expression/;
Re.test ($ ("txtID"). Val ())
Method Two:
$ ("txtID"). Val.match (/Regular expression/);

Expressions commonly used for validation: (Baidu collation)

Verification Number: ^[0-9]*$
To verify N-bit numbers: ^\d{n}$
Verify that at least n digits: ^\d{n,}$
Verify the number of m-n bits: ^\d{m,n}$
Verify numbers starting with 0 and non 0: ^ (0|[ 1-9][0-9]*) $
Verify that there is a positive real number with two decimal places: ^[0-9]+ (. [ 0-9]{2})? $
Verify that there is a positive real number with 1-3 decimal places: ^[0-9]+ (. [ 0-9]{1,3})? $
Verify non-zero positive integers: ^\+? [1-9] [0-9]*$
To verify a nonzero negative integer: ^\-[1-9][0-9]*$
Validates non-negative integers (positive integers + 0) ^\d+$
Validates a non-positive integer (negative integer + 0) ^ ((-\d+) | ( 0+)) $
Verify the character with a length of 3: ^. {3}$
Validates a string consisting of 26 English letters: ^[a-za-z]+$
Validates a string consisting of 26 uppercase English letters: ^[a-z]+$
Validates a string consisting of 26 lowercase English letters: ^[a-z]+$
Validates a string consisting of a number and 26 English letters: ^[a-za-z0-9]+$
Validates a string consisting of a number, 26 letters, or underscores: ^\w+$
Verify user password: ^[a-za-z]\w{5,17}$ the correct format is: Start with a letter, the length is between 6-18, and can contain only characters, numbers, and underscores.
Verify that it contains ^%& ',; =?$\ ' characters:[^%& ', =?$\x22]+
Verify Kanji: ^[\u4e00-\u9fa5],{0,}$
Verify email Address: ^\w+[-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$
Verify interneturl:^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)? $; ^[a-za-z]+://(w+ (-w+) *) (. ( w+ (-w+) *) * (? s*)? $
Verify the phone number: ^ (\ (\d{3,4}\) |\d{3,4}-)? \d{7,8}$:--the correct format is: xxxx-xxxxxxx,xxxx-xxxxxxxx,xxx-xxxxxxx,xxx-xxxxxxxx,xxxxxxx, XXXXXXXX.
Verify your Social Security number (15-bit or 18-digit number): ^\d{15}|\d{}18$
Validation 12 months of the year: ^ (0?[ 1-9]|1[0-2]) $ correct format: "01"-"09" and "1" "12"
Verify one months of 31 days: ^ ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ The correct format is: 01, 09 and 1, 31.
Integer: ^-?\d+$
Non-negative floating-point number (positive floating point + 0): ^\d+ (\.\d+)? $
Positive floating-point number ^ ([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)) $
Non-positive floating-point number (negative floating point + 0) ^ ((-\d+ (\.\d+)?) | (0+ (\.0+)?)) $
Negative floating-point number ^ (-([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))) $
Floating point ^ (-?\d+) (\.\d+)?

China postcode verification: match form: 215421, Regular expression "d{6}"

Chinese mobile phone number in extracting information: (*0*13\D{9)

JS in the example of the wording:

/determine if the input content is empty
function IsNull () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length==0) {
Alert (' Sorry, the text box cannot be empty or blank! '); /Please change the text box to the name of the property you need to verify!
}
}

Determine whether the date type is a type of YYYY-MM-DD format
function IsDate () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/;
var r = Str.match (reg);
if (r==null)
Alert (' Sorry, the date you entered is not in the correct format! '); Please change "date" to the name of the property you need to verify!
}
}

Type that determines whether the date type is YYYY-MM-DD hh:mm:ss format
function Isdatetime () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) $/;
var r = Str.match (reg);
if (r==null)
Alert (' Sorry, the date you entered is not in the correct format! '); Please change "date" to the name of the property you need to verify!
}
}

Determine whether the date type is a type of HH:MM:SS format
function Istime ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^ ((20|21|22|23|[ 0-1]\d) \:[0-5][0-9]) (\:[0-5][0-9])? $/
if (!reg.test (str)) {
Alert ("Sorry, the date you entered is not in the correct format!"); /Please change the "date" to the name of the property you need to verify!
}
}
}

Determine if the character entered is an English letter
function Isletter ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[a-za-z]+$/;
if (!reg.test (str)) {
Alert ("Sorry, the English letter type you typed is not properly formatted!"); /Please change the "English letter type" to the name of the property you need to verify!
}
}
}

Determines whether the input character is an integer
function Isinteger ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[-+]?\d*$/;
if (!reg.test (str)) {
Alert ("Sorry, the integer type you entered is not formatted correctly!"); /Please replace the "integer type" with the name of the property you want to verify!
}
}
}

Determines whether the input character is double precision
Function Isdouble (val)
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
Reg=/^[-\+]?\d+ (\.\d+) $/;
if (!reg.test (str)) {
Alert ("Sorry, the double type you entered is not in the correct format!"); /Please replace "double type" with the name of the property you want to verify!
}
}
}


Determine if the character entered is: a-z,a-z,0-9
function isstring ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[a-za-z0-9_]+$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determines whether the character entered is in Chinese
function Ischinese ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[\u0391-\uffe5]+$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determine if the input email format is correct
function Isemail ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determine if the input ZIP code (six-bit only) is correct
function Iszip ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^\d{6}$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Judging the number entered is not greater than a specific number
function MaxValue ()
{
var val = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[-+]?\d*$/;
if (!reg.test (str)) {//Determines whether a number type
if (Val>parseint (' 123 '))//"123" for the maximum value set for itself
{
Alert (' Sorry, the number you entered is out of range ');//Please change "number" to the name of the property you want to verify!
}
}
}
}

Call method: OnBlur () method (loses focus trigger), such as:

<input type= "text" id= "Age" name= "Age" onblur= "valage ()"/>


Regular expression syntax and validation

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.