Some common Regular Expressions in PHP

Source: Internet
Author: User

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: [s |] *
Regular Expressions matching HTML tags:/<(. *)>. * </> | <(. *)/>/
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 // The regular expression that matches the IP address
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 ))
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:

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.