Match double-byte characters (including Chinese characters): [^ \ x00-\ xFF]
Application: Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)
String. Prototype. Len = function () {return this. Replace ([^ \ x00-\ xFF]/g, "AA"). length ;}
Regular Expression for matching empty rows: \ n [\ s |] * \ r
Regular Expressions matching HTML tags:/<(. *)>. * <\/> | <(. *) \/>/
Regular Expression that matches spaces at the beginning and end: (^ \ s *) | (\ s * $)
Application: JavaScript does not have trim functions like VBScript. We can use this expression to implement it, as shown below:
String. Prototype. Trim = function (){
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}
Use regular expressions to break down and convert IP addresses:
The following describes how to use a regular expression to match an IP address and convert the IP address to a corresponding value in Javascript.Program:
Function ip2v (IP ){
Re =/(\ D +) \. (\ D +)/g // Regular Expression matching IP addresses
If (Re. Test (IP )){
Return Regexp. * Math. Pow (255, 3) + Regexp. * Math. Pow (255, 2) + Regexp. * 255 + Regexp. * 1
}
Else {
Throw new error ("not a valid IP address! ")
}
}
However, if the above program does not use regular expressions, it may be easier to directly use the split function to separate them. The program is as follows:
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 ))
The regular expression matching the email address: \ W + ([-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W + )*
The regular expression matching the URL: http: // ([\ W-] + \.) + [\ W-] + (/[\ W -./? % & =] *)?
The regular expression is used to remove repeated characters in the string.AlgorithmProgram:
VaR S = "abacabefgeeii"
VaR S1 = S. Replace (/(.). */g ,"")
VaR Re = new Regexp ("[" + S1 + "]", "G ")
VaR S2 = S. Replace (Re ,"")
Alert (S1 + S2) // The result is: abcefgi
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"
S = S. Replace (/(. * \/) ([^ \.] +). */ig ,"")
Alert (s)
Use regular expressions 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 ,''))"
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 ,''))"
You can only enter numbers using regular expressions:
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 ,''))