Dynamic Web pages make PHP regular expressions commonly used

Source: Internet
Author: User
Tags html tags numeric value pow regular expression

Matching regular expressions for Chinese characters: [U4E00-U9FA5]

Match Double-byte characters (including Chinese characters): [^x00-xff]

Application: Computes the length of the string (a double-byte character length meter 2,ascii character 1)

String.prototype.len=function () {return This.replace ([^x00-xff]/g, "AA"). Length;}

A regular expression that matches a blank row: n[s|] *r

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

Matching a regular expression with a trailing space: (^s*) | (s*$)

Application: JavaScript does not have a trim function like VBScript, we can use this expression to implement, as follows:

String.prototype.trim = function () {
Return This.replace (/(^s*) | ( s*$)/g, "");
}

To decompose and transform an IP address using a regular expression:

The following is a JavaScript program that uses a regular expression to match an IP address and converts an IP address to a corresponding numeric value:

function IP2V (IP) {
re=/(d). (d). (d). (d)/g//matching regular expressions for 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, and the split function directly to decompose may be simpler, 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 an email address: w ([-.] W) *@w ([-.] W) *.W ([-.] W) *

A regular expression that matches the URL of the URL: http://([w].) [W] (/[w-./?%&=]*)?

An algorithmic program that uses regular expressions to remove repetitive 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

JavaScript program that extracts the filename from the URL address using a regular expression, the following result is Page1

S= "Http://www.etoow.com/page1.htm"
S=s.replace (/(. */) ([^.]). */ig, "")
Alert (s)

Use regular expressions to restrict the entry of text boxes in a Web page's form:

Only Chinese can be entered with regular expression restrictions:

Onkeyup= "Value=value.replace (/[^u4e00-u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^u4e00-u9fa5]/g, ') "

Only full-width characters can be entered with regular expression restrictions:

Onkeyup= "Value=value.replace (/[^uff00-uffff]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^uff00-uffff]/g, ') "

Only numbers can be entered with regular expression restrictions:

Onkeyup= "Value=value.replace (/[^d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' Text '). Replace (/[^d]/g, ') "

You can only enter numbers and English with regular expression restrictions:

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.