Regular Expression Basic grammar detailed _ Regular expression

Source: Internet
Author: User
Tags date1 lowercase valid email address

A regular expression is a text pattern that includes ordinary characters (for example, letters between A and z) and special characters (called "metacharacters"). The pattern describes one or more strings to match when searching for text.

1. Basic Grammar of regular expressions

Two special symbols ' ^ ' and ' $ '. Their role is to point out the beginning and end of a string, respectively. Examples are as follows:

"^the": denotes all strings starting with "the" ("There", "the Cat", etc.);
"Of despair$": a string representing the end of "of despair";
"^abc$": means that the beginning and end are "abc" string-hehe, only "ABC" itself;
"Notice": Represents any string containing "notice".

Like the last example, if you don't use two special characters, you're saying that the string you want to find is in any part of the string you're looking for--you
Do not position it at a certain top.

and other ' * ', ' + ' and '? ' The three symbols that represent the number of occurrences of one or a sequence of characters. They represent "no or
More "," one or more "and" No or once ". Here are a few examples:

"ab*" means that a string has one followed by 0 or several B. ("A", "AB", "ABBB",...... );
"ab+" means that a string has a second followed by at least one B or more;
"Ab?" : Indicates that a string has a followed 0 or a B;
"a?b+$": indicates that there are 0 or one a followed by one or several B at the end of the string.

You can also use a range, enclosed in curly braces, to indicate the range of repeat times.

"Ab{2}": Indicates that a string has a followed 2 B ("ABB");
"Ab{2,}": Indicates that a string has a a followed by at least 2 B;
"ab{3,5}": Indicates that a string has a followed 3 to 5 B.

Please note that you must specify the lower bound of the range (for example: "{0,2}" instead of "{2}"). Also, you may have noticed, ' * ', ' + ' and
'?' Equivalent to "{0,}", "{1,}" and "{0,1}".
There is also a ' ¦ ', which indicates the "or" action:

"Hi¦hello": means "hi" or "Hello" in a string;
"(B¦CD) EF": means "bef" or "cdef";
"(a¦b) *c": Represents a string of "a" "B" mixed strings followed by a "C";

'.' You can override any character:

"A.[0-9]": Indicates that a string has a "a" followed by an arbitrary character and a number;
"^. {3}$ ": A string representing any three characters (3 characters in length);

The square brackets indicate that certain characters are allowed to appear at a specific position in a string:

"[AB]": Indicates that a string has a "a" or "B" (equivalent to "a¦b");
[A-d]: Indicates that a string contains one of the lowercase ' a ' to ' d ' (equivalent to "a¦b¦c¦d" or "[ABCD]");
"^[a-za-z]": Represents a string that begins with a letter;
"[0-9]%": Indicates a digit before a percent semicolon;
", [a-za-z0-9]$": Indicates that a string ends with a comma followed by a letter or number.

You can also use ' ^ ' in square brackets to indicate a character that you do not want to appear, ' ^ ' should be first in square brackets. (such as: "%[^a-za-z]%" table
The letter should not appear in the two percent sign.

In order to express it verbatim, you have to be in "^.$ () ¦*+?" {\ ' Precede these characters with the transfer character ' \ '.

Note that in square brackets, you do not need an escape character.

2. Regular expression validation controls the type of input character of a text box

1. Only numbers and English 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 full angle 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 are 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. Regular Expression Application example popular explanation

*******************************************************************************

The checksum is all made up of numbers

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

^ denotes the beginning character to match the rule immediately following ^

$ indicates the rule with the beginning character to match the preceding $

The content in [] is an optional character set

[0-9] Indicates a requirement for a range of characters between 0-9

{1,20} indicates that the length of the numeric string is valid from 1 to 20, that is, the number of characters in [0-9] occurs in the range of 1 to 20 times.
/^ and $/should be used to represent rules that require an exact match of the entire string, rather than just one substring in the string.
*******************************************************************************

Verify Login Name: Can only enter 5-20 letters beginning with a letter, can be with numbers, "_", "." The string

/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/

^[a-za-z]{1} indicates that the first character requirement is a letter.

([a-za-z0-9]| [._]) {4,19} represents a 4-to-9-bit string that starts at the second bit (since it immediately follows the previous expression), and requires that it be composed of uppercase and lowercase letters, numbers, or special character sets [. _].

*******************************************************************************

Verify user name: You can enter only 1-30 strings that start with a letter

/^[a-za-z]{1,30}$/

*******************************************************************************

Verify password: Can only enter 6-20 letters, numbers, underscores

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

\w: Used to match letters, numbers, or underscore characters

*******************************************************************************

Check ordinary telephone, fax number: You can "+" or the beginning of the number can contain "-" and ""

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

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

“?” The metacharacters specify that its leading object must appear 0 consecutive times or once in the target object

Strings that can be matched such as: +123-999 999; +123-999 999; 123 999 999; +123 999999 etc.

*******************************************************************************

Validate URL

/^http[s]{0,1}:\/\/.+$/or/^http[s]{0,1}:\/\/.{ 1,n}$/(indicates the length of the URL string ("https://") + N)

\/: Represents the character "/".

. Represents a set of all characters

+ is equal to {1,}, is 1 to positive infinity Bar.

*******************************************************************************

Checksum pure Chinese character
/^[\u4e00-\u9fa5]+$/
[\U4E00-\U9FA5]: Estimation is the range of Chinese characters The
above expressions are tested in the following JavaScript through
 
 

4. The principle of expression application

"^\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+)? $"//nonnegative floating-point number (positive float + 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 of 26 English letters
"^[a-z]+$"//A string of 26 uppercase letters
"^[a-z]+$"///a string consisting of 26 lowercase letters
"^[a-za-z0-9]+$"//A string of numbers and 26 English letters
"^\w+$"//A string of numbers, 26 English 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}$//mac address regular expression
^[-+]?\d+ (\.\d+)? $//value type regular expression

5.javascript Regular Expression test

Checksums are all composed of numbers function isdigit (s) {var patrn=/^[0-9]{1,20}$/; if (!patrn.exec (s)) return false return True}//Verify login: Only input 5-20 starts with a letter, can be with a number, "_", "." String function Isregisterusername (s) {var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[.
_]) {4,19}$/; if (!patrn.exec (s)) return false return True}//Verify user name: You can enter only 1-30 string function Istruename (s) {var patrn=/^[a-za-z]{1, beginning with a letter.
30}$/;
if (!patrn.exec (s)) return false return True}//Verify password: Can only enter 6-20 letters, digits, underline function ispasswd (s) {var patrn=/^ (\w) {6,20}$/; if (!patrn.exec (s)) return False to return true}//verify ordinary telephone, fax number: You can start with "+", except for numbers, can contain "-" function Istel (s) {//var patrn=/^[+]{0,1 } (\d) {1,3}[]? ([-]? (\d) {1,12})
+$/; var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) |
[]) {1,12}) +$/; if (!patrn.exec (s)) return false return True}//Check mobile number: Must start with a number, except for numbers, can contain "-" function Ismobil (s) {var patrn=/^[+]{0,1} (\d) {1,3} [ ]? ([-]? ((\d) |
[]) {1,12}) +$/; if (!patrn.exec (s)) return false return True}//Verify ZIP code function Ispostalcode (s) {//var patrn=/^[a-za-z0-9]{3,12}$/; var Patrn=/^[a-za-z0-9]{3,12}$/; if (!patrn.exec (s)) return false return True}//Checksum search keyword function Issearch (s) {var patrn=/^[^ ' ~!@#$%^&* () +=|\\\][\]\{ \}:;\ ' \,.<>/?] {1} [^ ' ~!@$%^& () +=|\\\] [\]\{\}:;\ ' \,.<>?]
{0,19}$/; if (!patrn.exec (s)) return false return True} function IsIP (s)//by zergling {var patrn=/^[0-9.]
{1,20}$/; if (!patrn.exec (s)) return false return True}/********************************************************************* * Function:isbetween * Parameters:val as any value * Lo as Lower limit to check * Hi as higher limit to Che
CK * calls:nothing * returns:true If Val is between Lo and hi both inclusive, otherwise false. /function Isbetween (Val, lo, hi ) {if (Val < lo) | | (val > Hi))
{return (false);}
else {return (true);}} /********************************************************************************* * Function:isdate checks a valid Date * Parameters:thestr AS String * calls:isbetween, Isint * returns:true If THESTR is a valid date otherwise false. /function IsDate (thestr) {var
the1st = Thestr.indexof ('-');
var the2nd = thestr.lastindexof ('-'); if (the1st = = the2nd) {return (false);} else {var y = thestr.substring (0,the1st); var m = thestr.substring (the1st+1,the2
nd);
var d = thestr.substring (the2nd+1,thestr.length);
var maxdays = 31; 
if (Isint (m) ==false | | isint (d) ==false | | isint (y) ==false) {return (false),} else if (Y.length < 4) {return (false);
else if (!isbetween (M, 1,)) {return (false);} else if (m==4 | | m==6 | | m==9 | | m==11) maxdays = 30; else if (m==2) {if (y% 4 > 0) maxdays = = else if (y% = 0 && y% > 0) maxdays =; else maxda
ys = 29;
} if (Isbetween (d, 1, maxdays) = = False) {return (false);} else {return (true);}} }
/*********************************************************************************
* Function:iseudate checks a valid date in British format * Parameters:thestr as String * calls:isbetween, Isint * RET
Urns:true If THESTR is a valid date otherwise false. /function Iseudate (THESTR) {if (Isbetween (Thestr.length, 8,) = = False)
{return (false);} else {var the1st = Thestr.indexof ('/'); var the2nd = Thestr.lastindexof ('/'); if (the1st = = the2nd) {return (false);} El Se {var m = thestr.substring (the1st+1,the2nd); var d = thestr.substring (0,the1st); var y = thestr.substring (the2nd+1,thes
Tr.length);
var maxdays = 31; 
if (Isint (m) ==false | | isint (d) ==false | | isint (y) ==false) {return (false),} else if (Y.length < 4) {return (false);
else if (Isbetween (M, 1,) = = False) {return (false);} else if (m==4 | | m==6 | | m==9 | | m==11) maxdays = 30; else if (m==2) {if (y% 4 > 0) maxdays = = else if (y% = 0 && y% > 0) maxdays =; else maxda
ys = 29; } if(Isbetween (d, 1, maxdays) = = False)
{return (false);}
else {return (true);}} }/******************************************************************************** * Function:compare Date!
Which is the latest! * Parameters:lessdate,moredate as String * calls:isdate,isbetween * returns:true if Lessdate<moredate ************** /function Iscomdate (lessdate, moredate) {if (!
IsDate (lessdate)) {return (false),} if (!isdate (moredate)) {return (false);} var less1st = Lessdate.indexof ('-');
var less2nd = lessdate.lastindexof ('-');
var more1st = moredate.indexof ('-');
var more2nd = moredate.lastindexof ('-');
var Lessy = lessdate.substring (0,less1st);
var LESSM = lessdate.substring (less1st+1,less2nd);
var LESSD = lessdate.substring (less2nd+1,lessdate.length);
var morey = moredate.substring (0,more1st);
var morem = moredate.substring (more1st+1,more2nd);
var mored = moredate.substring (more2nd+1,moredate.length); var Date1 = new Date (LESSY,LESSM,LESSD); 
var Date2 = new Date (morey,morem,mored);
if (Date1>date2) {return (false); return (true); }/********************************************************************************* * FUNCTION IsEmpty checks if the parameter is empty or null * parameter str as String ******************************************************************* /function IsEmpty (str) {if (str==null) | | (str.length==0))
return true;
else return (false); }/********************************************************************************* * Function:isint * PARAMETER: Thestr as String * returns:true If the passed parameter is a integer, otherwise FALSE * Calls:isdigit **************** /function Isint (THESTR) {var flag = true; if ( IsEmpty (THESTR)) {Flag=false} else {for (var i=0; i<thestr.length; i++) {if isdigit (thestr.substring (i,i+1)) = = F
Alse) {flag = false; break;}}
return (flag);
}/********************************************************************************* * Function:isreal * PARAMETER: Hestr as String Declen as Integer (how many digits after period) * Returns:true if Thestr is a float, otherwise FALSE * Calls:isint **********************************************************************************/function IsReal ( Thestr, Declen) {var dot1st = Thestr.indexof ('. '); var dot2nd = Thestr.lastindexof ('. '); var OK = true; if (IsEmpty
R)) return false;
if (dot1st = = 1) {if (!isint (THESTR)) return (false), else return (true),} else if (dot1st!= dot2nd) return (false);
else if (dot1st==0) return (false); else {var Intpart = thestr.substring (0, dot1st); var decpart = thestr.substring (dot2nd+1); if (Decpart.length > Declen
) return (false);
else if (!isint (intpart) | | |!isint (DECPART)) return (false);
else if (IsEmpty (Decpart)) return (false);
else return (true); }/********************************************************************************* * FUNCtion:isemail * parameter:string (email address) * Returns:true If the String are a valid email address * FALSE if the P Assed string is not a valid e-mail address * email format:anyname@emailserver e.g;
webmaster@hotmail.com * @ sign can appear only once in the e-mail address. /function Isemail (thestr) {var
Atindex = Thestr.indexof (' @ ');
var dotindex = Thestr.indexof ('. ', Atindex);
var flag = true; Thesub = thestr.substring (0, dotindex+1) if (Atindex < 1) | | (Atindex!= thestr.lastindexof (' @ ')) | | (Dotindex < Atindex + 2) | | 
(Thestr.length <= thesub.length))
{return (false);}
else {return (true);}} /********************************************************************************* * Function:newwindow * Parameters:doc-> Document to open in the new window Hite-> Height of the new window wide-> Width of the new W Indow bars-> 1-scroll bars = YES 0-scroll bars = NO Resize-> 1-resizable = YES 0-resizable = NO * calls:none * returns:new window instance ********************************************* /function NewWindow (DOC, Hite, wide, bars, resize) {var winnew= "_blank"; var opt= "
Toolbar=0,location=0,directories=0,status=0,menubar=0, ";
opt+= ("scrollbars=" +bars+ ",");
opt+= ("resizable=" +resize+ ",");
opt+= ("width=" +wide+ ",");
opt+= ("height=" +hite);
Winhandle=window.open (doc,winnew,opt);
Return }/********************************************************************************* * Function:decimalformat * Parameters:paramvalue-> Field Value * calls:none * returns:formated string ****************************************
/function DecimalFormat (paramvalue) {var intpart = parseint (paramvalue);
var decpart =parsefloat (paramvalue)-Intpart;
str = ""; if ((decpart = 0) | | (Decpart = null))
STR + + (Intpart + ". 00");
else str = (Intpart + decpart);
return (str); }

"^\\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+)? $"//nonnegative floating-point number (positive float + 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 of 26 English letters
"^[a-z]+$"//A string of 26 uppercase letters
"^[a-z]+$"///a string consisting of 26 lowercase letters
"^[a-za-z0-9]+$"//A string of numbers and 26 English letters
"^\\w+$"//A string of numbers, 26 English letters, or underscores
"^[\\w-]+ (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$ "//email address
"^[a-za-z]+://(\\w+ (-\\w+) *) (\ \\w+ (-\\w+) *)) * (\\?\\s*) $ "//url

The above is the entire content of this article, I hope you like.

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.