Regular Expression used for regular expression table expression set 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:/n [/S |] */R
Regular Expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/
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. $1 * Math. Pow (255) + Regexp. $2 * Math. Pow () + Regexp. $3 * + Regexp. $4*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 ))
Match the regular expression of 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 (/(.). */1/g, "$1 ")
VaR Re = new Regexp ("[" + S1 + "]", "G ")
VaR S2 = S. Replace (Re ,"")
Alert (S1 + S2) // The result is: abcefgi
Javascript programs that extract file names from URLs using regular expressions. the following result is page1.
S = "http://www.9499.net/page1.htm"
S = S. Replace (/(. * //) {0,} ([^/.] +). */ig, "$2 ")
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 the full-width characters: onkeyup = "value = value. replace (/[^/uff00-/Uffff]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^/uff00-/Uffff]/g ,''))"
Use a regular expression to limit that only numbers can be entered: 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 ,''))"
"^/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 + )? $ "// Non-negative floating point number (Positive 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] *) $ "// 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 consisting of 26 English letters
"^ [A-Z] + $" // a string consisting of 26 uppercase letters
"^ [A-Z] + $" // a string consisting of 26 lowercase letters
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters
"^/W + $" // a string consisting of digits, 26 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