Regular Expressions Java PHP Some conversions of some commonly used regular expression characters

Source: Internet
Author: User
Tags pow
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: \n[\s|] *\r
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//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, 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 matching 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:
Use regular expression restrictions to enter only Chinese:
/[^\u4e00-\u9fa5]/g, ') '/[^\u4e00-\u9fa5]/g, ') '
You can only enter full-width characters with regular expression restrictions:
/[^\uff00-\uffff]/g, ') '/[^\uff00-\uffff]/g, ') '
You can only enter numbers with regular expression restrictions:
/[^\d]/g, ') '/[^\d]/g, ') '
Use regular expression restrictions to enter only numbers and English:
/[\w]/g, ') '/[^\d]/g, ')

The above describes some of the regular expression Java PHP some commonly used regular expression character conversion, including the regular expression Java aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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