Regular expression matching the leading and trailing spaces: (^/s*) | (/s*$)
String.prototype.trim = function ()
{
Return This.replace (/(^/s*) | ( /s*$)/g,);
}
Use regular expressions to decompose and convert IP addresses:
The following is a JavaScript program that matches an IP address with a regular expression and translates an IP address into a corresponding value:
function IP2V (IP)
{
re=/(/d+)/. (/d+)/. (/d+)/. (/d+)/g//matching regular expressions for IP addresses
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, the above program without regular expression, and directly with the split function decomposition may be more simple, the program is 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 email address:/w+ ([-+.] /w+) *@/w+ ([-.] /w+) */./w+ ([-.] /w+) *
Regular expression matching URL URL:/http ([/w-]+/.) +[/w-]+ (/[/w-./?%&=]*)?
algorithm program for removing repeated characters in a string using regular expressions : [Note: This program is not correct, for the reasons see this post reply]
var s=abacabefgeeii
var s1=s.replace (/(.). */1/G,$1)
var re=new RegExp ([+s1+],g)
var s2=s.replace (Re,)
Alert (S1+S2)//Result: ABCEFGI
I used to post on the csdn to find an expression to implement the method of removing the repeated characters, which was not found, which is the simplest implementation I can think of. The idea is to use a back-to-reference to remove a repeating character, create a second expression with a repeating character, and take a concatenation of the characters that are not duplicated. This method may not be applicable for strings that require a character order.
A JavaScript program that uses a regular expression to extract the filename from the URL address, as shown in Page1
S=http://www.9499.net/page1.htm
S=s.replace (/(. *//) {0,} ([^/.] +). */ig,$2)
Alert (s)
Use regular expressions to restrict the entry of text boxes in Web Forms:
Use regular expression restrictions to enter only Chinese: 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 with regular expression restrictions: onkeyup=value=value.replace (/[^/uff00-/uffff]/g, ') onbeforepaste=clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/uff00-/uffff]/g, "))
Use regular expression restrictions to enter only numbers: Onkeyup=value=value.replace (/[^/d]/g, ') onbeforepaste=clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, "))
Use regular expression restrictions to enter only numbers and English: Onkeyup=value=value.replace (/[/w]/g, ') onbeforepaste=clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, "))
Regular expressions, RELATED links
Http://blog.csdn.net/laily/category/19548.aspx
Http://blog.csdn.net/laily/archive/2004/06/30/30525.aspx Microsoft Regular Expression Tutorial (V): Select/Group and back reference
Http://blog.csdn.net/laily/archive/2004/06/30/30522.aspx Microsoft's regular expression tutorial (iv): Qualifiers and Locators
Http://blog.csdn.net/laily/archive/2004/06/30/30517.aspx Microsoft's regular expression tutorial (iii): Character matching
Http://blog.csdn.net/laily/archive/2004/06/30/30514.aspx Microsoft's regular expression tutorial (ii): Regular expression syntax and precedence order
Http://blog.csdn.net/laily/archive/2004/06/30/30511.aspx Microsoft's regular expression tutorial (i): Introduction to Regular expressions
Http://blog.csdn.net/laily/archive/2004/06/30/30360.aspx Small Program: Advanced Find/Replace, regular expression practitioner, JavaScript Script Debugger
Http://blog.csdn.net/laily/archive/2004/06/24/25872.aspx Classic Regular Expressions
Regular expressions, regular expressions, regular expression matching, regular expression syntax, pattern matching, regular expression matching JavaScript regular expression asp regular expression. NET regular Expression C # Regular expression JSP regular expression php regular expression-VB. NET regular expression VBScript regular Expression programming delphi regular Expression JScript Javascript:__dopostback (' Comments.ascx$commentlist$_ctl0$editlink ', ' ) >
Regular Expressions Regular expression
Regular Expression RegExp
Mode pattern
Matching match
. NET namespace: System.Text.RegularExpression
Javascript:__dopostback (' Comments.ascx$commentlist$_ctl1$editlink ', ') >
Add:
^/d+$//matches non-negative integers (positive integers + 0)
^[0-9]*[1-9][0-9]*$//Match positive integer
^ ((-/d+) | (0+)) $//matches a non-positive integer (negative integer + 0)
^-[0-9]*[1-9][0-9]*$//Match negative integer
^-?/d+$//Match 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]*)) $//Match positive floating point number
^ ((-/d+ (/./d+)?) | (0+ (/.0+)?)) $//matches 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]*))) $//Match negative floating-point number
^ (-?/d+) (/./d+)? $//Match floating point number
^[a-za-z]+$//Match a string consisting of 26 English letters
^[a-z]+$//matches a string consisting of 26 uppercase letters
^[a-z]+$//matches a string consisting of 26 letters in lowercase
^[a-za-z0-9]+$//matches a string consisting of a number and 26 English letters
^/w+$//matches a string consisting of a number, 26 letters, or underscores
^[/w-]+ (/.[ /w-]+) *@[/w-]+ (/.[ /w-]+) +$//Match email address
^[a-za-z]+://Match (/w+ (-/w+) *) (/. ( /w+ (-/w+) *) * (/?/s*)? $//Match URL javascript:__dopostback (' Comments.ascx$commentlist$_ctl2$editlink ', ' ') >
An algorithmic program that uses regular expressions to remove repeated characters from 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)//Result: ABCEFGI
===============================
If var s = Abacabefggeeii
The result is not right, the result is: ABEICFGG
The ability of regular expressions is limited javascript:__dopostback (' Comments.ascx$commentlist$_ctl3$editlink ', ') >
Re:totoro
Thank you for your advice, this JavaScript regular expression program algorithm is really a problem, I will try to find a better way!!! Javascript:__dopostback (' Comments.ascx$commentlist$_ctl4$editlink ', ') >
1. Confirm valid e-mail format
The following code 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 false, but no other action is taken. You can use IsValidEmail to filter out e-mail addresses that contain invalid characters before the application stores the address in the database or appears in an ASP.
[Visual Basic]
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 #]
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. Clean up 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 erase potentially harmful characters 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]
Function CleanInput (Strin As String) as String
' Replace invalid characters with empty strings.
Return Regex.Replace (Strin, [^/w/[email protected]],)
End Function
[C #]
String CleanInput (String strin)
{
Replace invalid characters with empty strings.
Return Regex.Replace (Strin, @[^/w/[email Protected]],);
}
3. Change the date format
The following code example uses the Regex.Replace method to replace the date form of the mm/dd/yy with the Dd-mm-yy date form.
[Visual Basic]
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 #]
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 regex.replace substitution pattern. where substitution expression ${day} Inserts substrings captured by the (? <day> ...) group.
There are several static functions that allow you to use regular expression operations without creating an explicit regular expression object, and the Regex.Replace function is one of them. If you do not want to keep the compiled regular expression, this will bring you convenience
4. Extracting URL Information
The following code example uses Match.result to extract the protocol and port number from a URL. For example, "http://www.contoso.com:8080/letters/readme.html" will return "http:8080".
[Visual Basic]
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 #]
String Extension (string url)
{
Regex r = new Regex (@^ (? <proto>/w+)://[^/]+? (? <port>:/d+)?/,
regexoptions.compiled);
return R.match (URL). Result (${proto}${port});
}
Javascript:__dopostback (' Comments.ascx$commentlist$_ctl5$editlink ', ') >
Today, a netizen asked: How to use regular expressions to denote either a number or a letter is a letter can only be an alphanumeric it doesn't matter?
My answer is:
^[a-za-z]$|^/d+$ javascript:__dopostback (' Comments.ascx$commentlist$_ctl6$editlink ', ') >
Download from: http://blog.csdn.net/chenqi0701/article/details/2244993
Regular expression floating-point integer