Some common regular expressions in PHP

Source: Internet
Author: User
Some common regular expressions in PHP. read some common regular expressions in PHP and match the regular expressions of Chinese characters: [\ u4e00-\ u9fa5] to match double-byte characters (including Chinese characters ): [^ \ x00-\ xff] application: calculates the length of a String (two-byte length: 2, ASCII character: 1) String. prototype. len = function () {ret

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: \ n [\ s |] * \ r

Regular expressions matching HTML tags:/<(. *)>. * <\/> | <(. *) \/>/

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:

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:

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:

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:

Onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g ,''))"

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.