Regular Expressions matching Chinese characters:
Copy codeThe Code is as follows:
[\ U4e00-\ u9fa5]
Match double-byte characters (including Chinese characters ):
Copy codeThe Code is as follows:
[^ \ X00-\ xff]
Application: Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)
Copy codeThe Code is as follows:
String. prototype. len = function () {return this. replace ([^ \ x00-\ xff]/g, "aa"). length ;}
Regular Expression matching empty rows:
Copy codeThe Code is as follows:
\ N [\ s |] * \ r
Regular Expressions matching HTML tags:
Copy codeThe Code is as follows:
/<(. *)>. * <\/\ 1> | <(. *) \/>/
Regular Expression that matches the beginning and end spaces:
Copy codeThe Code is as follows:
(^ \ S *) | (\ s * $)
Application: j avascript does not have trim functions like v bscript, so we can use this expression to implement it, as shown below:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code 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:
Copy codeThe Code is as follows:
\ W + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*
The regular expression matching the URL:
Copy codeThe Code is as follows:
Http: // ([\ w-] + \.) + [\ w-] + (/[\ w -./? % & =] *)?
Algorithm program that uses regular expressions to remove repeated characters in a string: [* Note: This program is incorrect]
Copy codeThe Code is as follows:
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
* Note
====================================
If var s = "abacabefggeeii"
The result is incorrect. The result is: abeicfgg.
Limited capabilities of Regular Expressions
====================================
I used to post on CSDN to seek an expression to remove repeated characters, but I couldn't find it. This is the simplest implementation method I can think. The idea is to use the back-to-back reference to retrieve repeated characters, then create a second expression with repeated characters, get non-repeated characters, and connect the two. This method may not apply to strings with character order requirements.
Javascript programs that extract file names from URLs using regular expressions. the following result is page1.
Copy codeThe Code is as follows:
S = "http://blog.penner.cn/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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
Onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g ,''))"
Match a non-negative integer (positive integer + 0)
Copy codeThe Code is as follows:
^ \ D + $
Match a positive integer
Copy codeThe Code is as follows:
^ [0-9] * [1-9] [0-9] * $
Match non-positive integers (negative integers + 0)
Copy codeThe Code is as follows:
^ (-\ D +) | (0 +) $
Match a negative integer
Copy codeThe Code is as follows:
^-[0-9] * [1-9] [0-9] * $
Matching integer
Copy codeThe Code is as follows:
^ -? \ D + $
Match non-negative floating point number (Positive floating point number + 0)
Copy codeThe Code is as follows:
^ \ D + (\. \ d + )? $
Match Positive floating point number
Copy codeThe Code is as follows:
^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $
Match non-Positive floating point number (negative floating point number + 0)
Copy codeThe Code is as follows:
^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $
Match negative floating point number
Copy codeThe Code is as follows:
^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $
Match floating point number
Copy codeThe Code is as follows:
^ (-? \ D +) (\. \ d + )? $
Match a string consisting of 26 English letters
Copy codeThe Code is as follows:
^ [A-Za-z] + $
Matches a string consisting of 26 uppercase letters.
Copy codeThe Code is as follows:
^ [A-Z] + $
Match a string consisting of 26 lower-case letters
Copy codeThe Code is as follows:
^ [A-z] + $
Match strings consisting of digits and 26 English letters
Copy codeThe Code is as follows:
^ [A-Za-z0-9] + $
Match a string consisting of digits, 26 English letters, or underscores
Copy codeThe Code is as follows:
^ \ W + $
Matching email address
Copy codeThe Code is as follows:
^ [\ W-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $
Matching url
Copy codeThe Code is as follows:
^ [A-zA-z] +: // match (\ w + (-\ w + )*)(\. (\ w + (-\ w + )*))*(\? \ S *)? $
Matching html tags
Copy codeThe Code is as follows:
<\ S * (\ S +) (\ s [^>] *)?> (.*?) <\ S * \/\ 1 \ s *>
Visual Basic & C # Regular Expression
1. Confirm the valid email format
The following example uses the static Regex. IsMatch method to verify whether a string is in a valid email format. If the string contains a valid email address, the IsValidEmail method returns true; otherwise, the return value is false without any other operations. You can use IsValidEmail to filter out email addresses that contain invalid characters before the application stores the addresses in the database or displays them on the ASP. NET page.
[Visual Basic]
Copy codeThe Code is as follows:
Function IsValidEmail (strIn As String) As Boolean
'Return true if strIn is in valid e-mail format.
Return Regex. isMatch (strIn, ("^ ([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ")
End Function
[C #]
Copy codeThe Code is as follows:
Bool IsValidEmail (string strIn)
{
// Return true if strIn is in valid e-mail format.
Return Regex. isMatch (strIn, @ "^ ([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ");
}
2. Clear input strings
The following code example uses the static Regex. Replace method to extract invalid characters from the string. You can use the CleanInput method defined here to clear potentially harmful characters that may be entered in the text field of the form that receives user input. CleanInput returns a string after all non-alphanumeric characters except @,-(hyphen), and. (period) are cleared.
[Visual Basic]
Copy codeThe Code is as follows:
Function CleanInput (strIn As String) As String
'Replace invalid characters with empty strings.
Return Regex. Replace (strIn, "[^ \ w \. @-]", "")
End Function
[C #]
Copy codeThe Code is as follows:
String CleanInput (string strIn)
{
// Replace invalid characters with empty strings.
Return Regex. Replace (strIn, @ "[^ \ w \. @-]", "");
}
3. Change the Date Format
The following code uses the Regex. Replace method to Replace the date format of mm/dd/yy with the date format of dd-mm-yy.
[Visual Basic]
Copy codeThe Code is as follows:
Function MDYToDMY (input As String) As String
Return Regex. Replace (input ,_
"\ B (? <Month> \ d {1, 2 })/(? <Day> \ d {1, 2 })/(? <Year> \ d {2, 4}) \ B ",_
"$ {Day}-$ {month}-$ {year }")
End Function
[C #]
Copy codeThe Code is as follows:
String MDYToDMY (String input)
{
Return Regex. Replace (input, "\ B (? <Month> \ d {1, 2 })/(? <Day> \ d {1, 2 })/(? <Year> \ d {2, 4}) \ B "," $ {day}-$ {month}-$ {year }");
}
Regex replacement Mode
This example shows how to use a named reverse reference in the Replace mode of Regex. Replace. Here, the replace expression $ {day} is inserted (?...) The substring captured by the Group.
There are several static functions that allow you to operate using regular expressions without creating explicit Regular Expression objects. The Regex. Replace function is one of them. If you do not want to retain the compiled regular expression, this will bring you convenience.
4. Extract URL Information
The following code uses Match. Result to extract the protocol and port number from the URL. For example, "http://www.penner.cn: 8080 ...... "Http: 8080" is returned ".
[Visual Basic]
Copy codeThe Code is as follows:
Function Extension (url As String) As String
Dim r As New Regex ("^ (? <Proto> \ w +): // [^/] +? (? <Port>: \ d + )? /",_
RegexOptions. Compiled)
Return r. Match (url). Result ("$ {proto }$ {port }")
End Function
[C #]
Copy codeThe Code is as follows:
String Extension (String url)
{
Regex r = new Regex (@ "^ (? <Proto> \ w +): // [^/] +? (? <Port>: \ d + )? /",
RegexOptions. Compiled );
Return r. Match (url). Result ("$ {proto }$ {port }");
}
A regular expression that contains only letters and numbers, no less than 6 characters, and numbers and letters.
In C #, this can be used to represent:
Copy codeThe Code is as follows:
"\ W {6} (\ w + )*"
An algorithm program that splits the path string into the root directory and sub-directories. The path formats include C: \ aa \ bb \ cc, \ aa \ bb \ cc, the ftp://aa.bb/cc above path will be split into: C: \ and aa \ bb \ cc, \ aa and \ bb \ cc, ftp: // and aa. bb/cc is implemented using javascript as follows:
Copy codeThe Code is as follows:
Var strRoot, strSub
Var regPathParse =/^ ([^ \/] + [\/] + |\\ \ [^ \] +) (. *) $/
If (regPathParse. test (strFolder ))
{
StrRoot = RegExp. $1
StrSub = RegExp. $2
}