Regular Expression matching Chinese characters: [u4e00-u9fa5]
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: [s |] *
Regular Expressions matching HTML tags:/<(. *)>. * </> | <(. *)/>/
Regular Expression matching 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 is a javascript program that uses regular expressions to match IP addresses and convert IP addresses to corresponding values:
Function IP2V (ip ){
Re =/(d). (d)/g // The regular expression that matches the IP address
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 ))
Regular Expression matching the Email address: w ([-.] w) * @ w ([-.] w) *. w ([-.] w )*
The regular expression matching the URL: http: // ([w-].) [w-] (/[w -./? % & =] *)?
Algorithm program that uses regular expressions to remove repeated characters in a string:
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: