A regular expression that matches a Chinese character:
Copy Code code as follows:
Match Double-byte characters (including Chinese characters):
Copy Code code as follows:
Application: Computes the length of the string (a double-byte character length meter 2,ascii character 1)
Copy Code code as follows:
String.prototype.len=function () {return This.replace ([^\x00-\xff]/g, "AA"). Length;}
A regular expression that matches a blank row:
Copy Code code as follows:
A regular expression that matches an HTML tag:
Copy Code code as follows:
/< (. *) >.*<\/\1>|< (. *) \/>/
A regular expression that matches a trailing space:
Copy Code code as follows:
Application: There is no trim function like V bscript in J Avascript, we can use this expression to implement, as follows:
Copy Code code as follows:
String.prototype.trim = function ()
{
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}
Decomposition and conversion of IP addresses with regular expressions
The following is a JavaScript program that uses a regular expression to match an IP address and converts an IP address to a corresponding numeric value:
Copy Code code as follows:
function IP2V (IP)
{
re=/(\d+) \. (\d+) \. (\d+) \. (\d+)/g//matching the regular expression of the IP address
if (Re.test (IP))
{
Return Regexp.$1*math.pow (255,3)) +regexp.$2*math.pow (255,2)) +regexp.$3*255+regexp.$4*1
}
Else
{
throw new Error ("not a valid IP address!")
}
}
However, if the above program does not use a regular expression, and the split function directly to decompose may be simpler, the program is as follows:
Copy Code code as follows:
var ip= "10.100.20.168"
Ip=ip.split (".")
Alert ("IP value is:" + (IP[0]*255*255*255+IP[1]*255*255+IP[2]*255+IP[3]*1))
Regular expression matching an email address:
Copy Code code as follows:
\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
A regular expression that matches the URL of the URL:
Copy Code code as follows:
http://([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)?
An algorithmic program that uses regular expressions to remove repetitive characters from a string: [* Note: This program is incorrect]
Copy Code code as follows:
var s= "Abacabefgeeii"
var s1=s.replace (/(.). *\1/g, "$")
var re=new RegExp ("[" +s1+ "]", "G")
var s2=s.replace (Re, "")
Alert (S1+S2)//Result: ABCEFGI
* Note
===============================
If var s = "Abacabefggeeii"
The result is wrong, the result is: ABEICFGG
The ability of regular expressions is limited
===============================
I used to post on the csdn to find an expression to achieve the elimination of repeated characters, and ultimately did not find, this is the simplest way I can think of implementation. The idea is to use a back reference to take out the characters that contain duplicates, and then to create a second expression with a repeating character, with a concatenation of the characters that are not repeated. This method may not apply to strings that are required for character order.
You have to use regular expressions to extract the filename from the URL address of the JavaScript program, the following result is Page1
Copy Code code as follows:
S= "Http://blog.penner.cn/page1.htm"
S=s.replace (/(. *\/) {0,} ([^\.] +). */ig, "$"
Alert (s)
Use regular expressions to restrict the entry of text boxes in a Web page's form:
Only Chinese can be entered with regular expression restrictions:
Copy Code code as follows:
Onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "
Only full-width characters can be entered with regular expression restrictions:
Copy Code code as follows:
Onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ') "
Only numbers can be entered with regular expression restrictions:
Copy Code code 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 with regular expression restrictions:
Copy Code code as follows:
Onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' Text '). Replace (/[^\d]/g, ') "
Match nonnegative integer (positive integer + 0)
Copy Code code as follows:
Match positive integer
Copy Code code as follows:
Match a non positive integer (negative integer + 0)
Copy Code code as follows:
Match a negative integer
Copy Code code as follows:
Match integer
Copy Code code as follows:
matching nonnegative floating-point numbers (positive floating-point number + 0)
Copy Code code as follows:
Matching positive floating-point numbers
Copy Code code as follows:
^ ([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $
Matching non-positive floating-point numbers (negative floating-point number + 0)
Copy Code code as follows:
^ ((-\d+ (\.\d+)?) | (0+ (\.0+)) $
Matching negative floating-point numbers
Copy Code code as follows:
^ (-([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $
Matching floating-point numbers
Copy Code code as follows:
Match a string of 26 English letters
Copy Code code as follows:
Match a string of 26 uppercase letters
Copy Code code as follows:
Match a string consisting of 26 lowercase letters
Copy Code code as follows:
Match a string of numbers and 26 English letters
Copy Code code as follows:
Match a string of numbers, 26 English letters, or underscores
Copy Code code as follows:
Match email address
Copy Code code as follows:
^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$
Match URL
Copy Code code as follows:
^[a-za-z]+://Match (\w+ (-\w+) *) (\. ( \w+ (-\w+) *)) * (\?\s*)? $
Match HTML tag
Copy Code code as follows:
<\s* (\s+) (\s[^>]*)?> (. *?) <\s*\/\1\s*>
Visual Basic & C # Regular Expression
1. Confirm valid e-mail format
The following example uses the static Regex.IsMatch method to verify whether a string is a valid e-mail format. If the string contains a valid e-mail address, the IsValidEmail method returns True, otherwise it returns false without taking any other action. You can use IsValidEmail to filter e-mail addresses that contain invalid characters before an application stores the address in a database or displays it in a asp.net page.
[Visual Basic]
Copy Code code as follows:
Function IsValidEmail (Strin as String) as Boolean
' Return True if Strin are 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 Code code as follows:
BOOL IsValidEmail (String strin)
{
Return true if Strin are 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. Clean the input string
The following code example uses the static Regex.Replace method to extract invalid characters from a string. You can use the CleanInput method defined here to clear out potentially harmful characters that are entered in the Text field of a form that accepts user input. CleanInput returns a string after all non-alphanumeric characters except the @,-(hyphen) and. (period) are cleared.
[Visual Basic]
Copy Code code as follows:
Function CleanInput (Strin As String) as String
' Replace invalid characters with empty strings.
Return Regex.Replace (Strin, "[^\w\.@-]", "")
End Function
[C #]
Copy Code code as follows:
String CleanInput (String strin)
{
Replace invalid characters with empty strings.
Return Regex.Replace (Strin, @ "[^\w\.@-]", "");
}
3. Change date format
The following code example uses the Regex.Replace method to replace the Mm/dd/yy date form with the Dd-mm-yy date form.
[Visual Basic]
Copy Code code 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 Code code 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 substitution mode
This example shows how to use a named reverse reference in Regex.Replace replacement mode. Where the substitution expression ${Day} Inserts a substring captured by the (?...) group.
There are several static functions that allow you to use a regular expression operation without creating an explicit regular expression object, and the Regex.Replace function is one of them. This will be convenient if you do not want to keep the compiled regular expressions
4. Extract URL Information
The following code example uses Match.result to extract the protocol and port number from the URL. For example, "http://www.penner.cn:8080 ... will return "http:8080".
[Visual Basic]
Copy Code code 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 Code code as follows:
String Extension (string url)
{
Regex r = new Regex (@ "^ <proto>\w+)://[^/]+? <port>:\d+)?/",
regexoptions.compiled);
return R.match (URL). Result ("${Proto}${Port}");
}
Regular expressions with letters and numbers, no less than 6 digits, and passwords contained in numeric letters
In C #, you can use this to represent:
Copy Code code as follows:
An algorithmic program that will need to split the path string into two parts of the root and subdirectory, taking into account the path format: C:\AA\BB\CC, \\AA\BB\CC, ftp://aa.bb/cc the above paths will be split into: C:\ and AA\BB\CC, \\aa and \bb\ CC, ftp://and AA.BB/CC are implemented using JavaScript as follows:
Copy Code code as follows:
var strroot,strsub
var regpathparse=/^ ([^\\^\/]+[\\\/]+|\\\\[^\\]+) (. *) $/
if (Regpathparse.test (strfolder))
{
Strroot=regexp.$1
Strsub=regexp.$2
}