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: \ 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 // 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! ")
}
}
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 -./? % & =] *)?
An algorithm program that uses regular expressions to remove repeated characters in a string: [Note: This program is incorrect. For the reason, see this post.]
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
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 + $ // match a non-negative integer (positive integer + 0)
^ [0-9] * [1-9] [0-9] * $ // match a positive integer
^ (-\ D +) | (0 +) $ // match a non-positive integer (negative integer + 0)
^-[0-9] * [1-9] [0-9] * $ // match a negative integer
^ -? \ D + $ // match the integer
^ \ D + (\. \ d + )? $ // Match 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] *) $ // matches the Positive floating point number
^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $ // Match a 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] *) $ // matches a negative floating point number.
^ (-? \ D +) (\. \ d + )? $ // Match floating point numbers
^ [A-Za-z] + $ // match A string consisting of 26 English letters
^ [A-Z] + $ // match a string consisting of 26 uppercase letters
^ [A-z] + $ // match a string consisting of 26 lowercase letters
^ [A-Za-z0-9] + $ // match a string consisting of digits and 26 letters
^ \ W + $ // match a string consisting of digits, 26 English letters, or underscores
^ [\ W-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ // match the email address
^ [A-zA-z] +: // match (\ w + (-\ w + )*)(\. (\ w + (-\ w + )*))*(\? \ S *)? $ // Match the url