PHP Some common regular expressions _php tutorial

Source: Internet
Author: User
Regular expressions that match Chinese characters: [U4E00-U9FA5]
Match double-byte characters (including kanji): [^x00-xff]
Application: Calculates the length of a string (a double-byte character length meter 2,ascii character 1)

String.prototype.len=function () {return This.replace ([^x00-xff]/g, "AA"). Length;
Regular expression that matches a blank line: [s|] *
Regular expression matching HTML tags:/< (. *) >.*|< (. *)/>/
Regular expression matching the leading and trailing spaces: (^s*) | (s*$)
Application: There is no trim function like VBScript in JavaScript, and we can use this expression to do the following:

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//matched regular expression of 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, 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 that matches the email address: w ([-.] W) *@w ([-.] W) *.W ([-.] W) *
Regular expression matching URL URL:/http ([W].) [W] (/[w-./?%&=]*)?
An algorithmic program that uses regular expressions to remove repeated characters from a string:

var s= "Abacabefgeeii"
var s1=s.replace (/(.). */g, "")
var re=new RegExp ("[" S1 "]", "G")
var s2=s.replace (Re, "")
Alert (S1 s2)//Result: ABCEFGI
A JavaScript program that extracts file names from URL addresses with regular expressions, with the following result as Page1

S= "Http://www.9499.net/page1.htm"
S=s.replace (/(. */) ([^.]). */ig, "")
Alert (s)
Use regular expressions to restrict the entry of text boxes in Web Forms:

http://www.bkjia.com/PHPjc/445169.html www.bkjia.com true http://www.bkjia.com/PHPjc/445169.html techarticle regular expression matching Chinese characters: [U4e00-u9fa5] matches double-byte characters (including kanji): [^x00-xff] Apply: Calculates the length of a string (a double-byte character length meter 2,a ...

  • 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.