Regular Expressions are often used in projects, but it takes some time to write them. Therefore, you do not have to write some common Regular Expressions each time. Gentleman is good at things and uses existing experience materials to improve programming efficiency.
Regular Expressions matching Chinese characters:
[\ U4e00-\ u9fa5]
[\ U4e00-\ u9fa5]
Match double-byte characters (including Chinese characters ):
[^ \ X00-\ xff]
[^ \ X00-\ xff]
Match the carriage return line break:
[(\ R \ n)]
[(\ R \ n)]
Whether the matching file path is legal:
[^ ([A-zA-Z] :) | (\\{ 2} \ w +) \$ ?) (<A href = "file: // \\ (\ w [\ w" >\\ (\ w [\ w </a>] *. *)]
[^ ([A-zA-Z] :) | (\\{ 2} \ w +) \$ ?) (<A href = "file: // \\ (\ w [\ w" >\\ (\ w [\ w </a>] *. *)]
Whether the match is a number:
! (Regex. IsMatch (object, @ "^ \ d + $") // It indicates that it is not a full number.
! (Regex. IsMatch (object, @ "^ \ d + $") // It indicates that it is not a full number.
Regular Expression matching empty rows:
\ N [\ s |] * \ r
\ N [\ s |] * \ r
Regular Expressions matching HTML tags:
/<(. *)>. * <\/\ 1> | <(. *) \/>/
/<(. *)>. * <\/\ 1> | <(. *) \/>/
Regular Expression that matches the beginning and end spaces:
(^ \ S *) | (\ s * $)
(^ \ S *) | (\ s * $)
Match the regular expression of the Email address:
\ W + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*
\ W + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*
@ "^ \ S * ([A-Za-z0-9 _-] + (\. \ w +) * @ (\ w + \.) + \ w {2, 3}) \ s * $"
@ "^ \ S * ([A-Za-z0-9 _-] + (\. \ w +) * @ (\ w + \.) + \ w {2, 3}) \ s * $"
@ "^ \ W + (-\ w +) | (\. \ w +) * \ @ {1} \ w + \. {1} \ w {2, 4 }(\. {0, 1} \ w {2}) {0, 1 }"
@ "^ \ W + (-\ w +) | (\. \ w +) * \ @ {1} \ w + \. {1} \ w {2, 4 }(\. {0, 1} \ w {2}) {0, 1 }"
The regular expression matching the URL:
Http: // ([\ w-] + \.) + [\ w-] + (/[\ w -./? % & =] *)?
Http: // ([\ w-] + \.) + [\ w-] + (/[\ w -./? % & =] *)?
The regular expression matching the phone number:
@ "^ (\ D {3, 4} \) | \ d {3, 4 }-?)? \ D {7, 8} $"
@ "^ (\ D {3, 4} \) | \ d {3, 4 }-?)? \ D {7, 8} $"
Regular Expression matching mobile phone number:
Common: @ "^ 1 [0-9] {10} $"
Common: @ "^ 1 [0-9] {10} $"
/* Regular expression that describes the mobile phone number rules of different companies
** Cmcc-China Mobile Phone Number rules
** Cucc-China Unicom mobile phone number rules
** Cnc-China Netcom 3G mobile phone number Rule */
String cmcc = "^ [1] {1} ([3] {1} [4-9] {1 }) | ([5] {1} [89] {1}) [0-9] {8} $"
String cucc = "^ [1] {1} ([3] {1} [0-3] {1 }) | ([5] {1} [3] {1}) [0-9] {8} $"
String cnc = "^ [1] {1} [8] {1} [89] {1} [0-9] {8} $"
/* Regular expression that describes the mobile phone number rules of different companies
** Cmcc-China Mobile Phone Number rules
** Cucc-China Unicom mobile phone number rules
** Cnc-China Netcom 3G mobile phone number Rule */
String cmcc = "^ [1] {1} ([3] {1} [4-9] {1 }) | ([5] {1} [89] {1}) [0-9] {8} $"
String cucc = "^ [1] {1} ([3] {1} [0-3] {1 }) | ([5] {1} [3] {1}) [0-9] {8} $"
String cnc = "^ [1] {1} [8] {1} [89] {1} [0-9] {8} $"
The regular expression that matches the ID number:
Generic match, @ "^ (\ d {14} | \ d {17}) (\ d | [xX]) $"
Generic match, @ "^ (\ d {14} | \ d {17}) (\ d | [xX]) $"
An example of exact match, however, can only match,
@ "^ (1 [1-5]) | (2 [1-3]) | (3 [1-7]) | (4 [1-6]) | (5 [0-4]) | (6 [1-5]) | 71 | (8 [12]) | 91) \ d {4} (19 \ d {2} (0 [13-9] | 1 [012]) (0 [1-9] | [12] \ d | 30) | (19 \ d {2} (0 [13578] | 1 [02]) 31) | (19 \ d {2} 02 (0 [1-9] | 1 \ d | 2 [0-8]) | (19 ([13579] [26] | [2468] [048] | 0 [48]) 0229) \ d {3} (\ d | X | x )? $"
An example of exact match, however, can only match,
@ "^ (1 [1-5]) | (2 [1-3]) | (3 [1-7]) | (4 [1-6]) | (5 [0-4]) | (6 [1-5]) | 71 | (8 [12]) | 91) \ d {4} (19 \ d {2} (0 [13-9] | 1 [012]) (0 [1-9] | [12] \ d | 30) | (19 \ d {2} (0 [13578] | 1 [02]) 31) | (19 \ d {2} 02 (0 [1-9] | 1 \ d | 2 [0-8]) | (19 ([13579] [26] | [2468] [048] | 0 [48]) 0229) \ d {3} (\ d | X | x )? $"
Regular Expression matching license plate number:
/^ Shaanxi [A-M]-[A-Z0-9] [0-9] {4} $/
/^ Shaanxi [A-M]-[A-Z0-9] [0-9] {4} $/
Use regular expressions to implement functions similar to trim in javascript:
String. prototype. trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
String. prototype. trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
Use a regular expression to calculate the length of a string (a dual-byte length is measured in 2 and ASCII characters are counted in 1 ):
String. prototype. len = function () {return this. replace ([^ \ x00-\ xff]/g, "aa"). length ;}
String. prototype. len = function () {return this. replace ([^ \ x00-\ xff]/g, "aa"). length ;}
Use regular expressions to break down and convert IP addresses:
1. Use a regular expression to match an IP address and convert the IP address to a corresponding value in Javascript:
Function IP2V (ip)
{
Re =/(\ d +) \. (\ d +)/g // Regular Expression matching IP addresses
If (re. test (ip ))
{
Return RegExp. $1 * Math. pow (255) + RegExp. $2 * Math. pow () + RegExp. $3 * + RegExp. $4*1
}
Else
{
Throw new Error ("Not a valid IP address! ")
}
}
Function IP2V (ip)
{
Re =/(\ d +) \. (\ d +)/g // Regular Expression matching IP addresses
If (re. test (ip ))
{
Return RegExp. $1 * Math. pow (255) + RegExp. $2 * Math. pow () + RegExp. $3 * + RegExp. $4*1
}
Else
{
Throw new Error ("Not a valid IP address! ")
}
}
2. directly use the split function Ip Address:
Var ip = "10.100.0000168"
Ipip = ip. split (".")
Alert ("the IP value is: "+ (ip [0] * 255*255*255 + ip [1] * 255*255 + ip [2] * 255 + ip [3] * 1 ))
Var ip = "10.100.0000168"
Ip = ip. split (".")
Alert ("the IP value is: "+ (ip [0] * 255*255*255 + ip [1] * 255*255 + ip [2] * 255 + ip [3] * 1 ))
Use a regular expression to extract a javascript program with a file name from a URL. the following result is page1.
S = "http://www.9499.net/page1.htm"
Ss = s. replace (/(. * \/) {0,} ([^ \.] +). */ig, "$2 ")
Alert (s)
S = "http://www.9499.net/page1.htm"
S = s. replace (/(. * \/) {0,} ([^ \.] +). */ig, "$2 ")
Alert (s)
Use a regular expression to restrict text box input in a webpage form.
You can only enter Chinese characters using regular expressions:
Onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g ,'')"
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ u4E00-\ u9FA5]/g ,''))"
Onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g ,'')"
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ u4E00-\ u9FA5]/g ,''))"
You can only enter full-width characters using regular expressions:
Onkeyup = "value = value. replace (/[^ \ uFF00-\ uFFFF]/g ,'')"
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ uFF00-\ uFFFF]/g ,''))"
Onkeyup = "value = value. replace (/[^ \ uFF00-\ uFFFF]/g ,'')"
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ uFF00-\ uFFFF]/g ,''))"
You can only enter numbers using regular expressions:
Onkeyup = "value = value. replace (/[^ \ d]/g ,'')"
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ d]/g ,''))"
Onkeyup = "value = value. replace (/[^ \ d]/g ,'')"
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ d]/g ,''))"
You can only enter numbers and English letters using regular expressions:
Onkeyup = "value = value. replace (/[\ W]/g ,'')"
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ d]/g ,''))"
Onkeyup = "value = value. replace (/[\ W]/g ,'')"
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ d]/g ,''))"
Use a regular expression to intercept content between specified characters:
Regex regx = new Regex (@ "([\ w \ W] *?) End ");
Match mach = regx. Match (@ "string to be intercepted ");
String str = mach. Value;
Str = Regex. Replace (str, @ "start ","");
Str = Regex. Replace (str, @ "end ","");
Regex regx = new Regex (@ "([\ w \ W] *?) End ");
Match mach = regx. Match (@ "string to be intercepted ");
String str = mach. Value;
Str = Regex. Replace (str, @ "start ","");
Str = Regex. Replace (str, @ "end ","");
For example, if you have a string: 'csdn blog channel', you want to extract the word 'blog 'as follows:
Regex regx = new Regex (@ "CSDN ([\ w \ W] *?) Channel ");
Match mach = regx. Match (@ "CSDN blog channel ");
String str = mach. Value;
Str = Regex. Replace (str, @ "CSDN ","");
Str = Regex. Replace (str, @ "channel ","");
Regex regx = new Regex (@ "CSDN ([\ w \ W] *?) Channel ");
Match mach = regx. Match (@ "CSDN blog channel ");
String str = mach. Value;
Str = Regex. Replace (str, @ "CSDN ","");
Str = Regex. Replace (str, @ "channel ","");
Result: str = "blog ";
From Poplar