Regular expression that matches 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. * Math. pow (255, 3) + RegExp. * Math. pow (255, 2) + RegExp. * 255 + RegExp. * 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 -./? % & =] *)?
Algorithm program that uses regular expressions to remove repeated characters in a string:
Var s = "abacabefgeeii"
Var s1 = s. replace (/(.). */g ,"")
Var re = new RegExp ("[" + s1 + "]", "g ")
Var s2 = s. replace (re ,"")
Alert (s1 + s2) // The result is: abcefgi
Use a regular expression to extract a javascript program with a file name from a URL. the following result is page1.
S = "http://www.9499.net/page1.htm"
S = s. replace (/(. * \/) ([^ \.] +). */ig ,"")
Alert (s)
Use regular expressions to restrict text box input in a webpage form:
You can only enter Chinese characters using regular expressions:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service