The Encyclopedia of Regular Expressions

Source: Internet
Author: User
Tags expression html tags integer numeric value php regular expression pow split trim
Regular summary: Collects some commonly used regular expressions.

Regular expressions are used for string processing, form validation, and so on, practical and efficient, but often not too sure to use, so it is always necessary to search the Internet. I've collected some commonly used expressions here for the purposes of my memo. This post will be updated at any time.

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:/< (. *) >.*<\/\1>|< (. *) \/>/

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 the regular expression of the IP address
if (Re.test (IP))
{
Return Regexp.$1*math.pow (255,3)) +regexp.$2*math.pow (255,2)) +regexp.$3*255+regexp.$4*1
}
Else
{
throw new Error ("not a valid IP address!")
}
}

However, if the above program does not use a regular expression, 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 URL URLs: http://([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)?

Using regular expressions to remove the repeated characters in the string algorithm program: [Note: This program is incorrect, the reason see this post reply]

var s= "Abacabefgeeii"
var s1=s.replace (/(.). *\1/g, "$")
var re=new RegExp ("[" +s1+ "]", "G")
var s2=s.replace (Re, "")
Alert (S1+S2)//Result: ABCEFGI

I used to post on the csdn to find an expression to achieve the elimination of repeated characters, and ultimately did not find, this is the simplest way I can think of implementation. The idea is to use a back reference to take out the characters that contain duplicates, and then to create a second expression with a repeating character, with a concatenation of the characters that are not repeated. This method may not apply to strings that are required for character order.

You have to use regular expressions to extract the filename from the URL address of the JavaScript program, the following result is Page1

S= "Http://www.9499.net/page1.htm"
S=s.replace (/(. *\/) {0,} ([^\.] +). */ig, "$"
Alert (s)

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

The regular expression limit can only be entered in Chinese: 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, ') "

Only numbers and English can be entered with regular expression restrictions: onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "





Regular expressions, RELATED links
Http://blog.csdn.net/laily/category/19548.aspx
Http://blog.csdn.net/laily/archive/2004/06/30/30525.aspx Microsoft's regular expression tutorial (V): selection/grouping and back references

Http://blog.csdn.net/laily/archive/2004/06/30/30522.aspx Microsoft's regular expression tutorial (iv): Qualifiers and Locators

Http://blog.csdn.net/laily/archive/2004/06/30/30517.aspx Microsoft's regular expression tutorial (iii): Character matching

Http://blog.csdn.net/laily/archive/2004/06/30/30514.aspx Microsoft's regular expression tutorial (ii): Regular expression syntax and order of precedence

Http://blog.csdn.net/laily/archive/2004/06/30/30511.aspx Microsoft's regular expression tutorial (i): Introduction to Regular expressions

Http://blog.csdn.net/laily/archive/2004/06/30/30360.aspx Applet: Advanced Find/Replace, regular expression practitioner, JavaScript scripting debugger

Classical regular expression of http://blog.csdn.net/laily/archive/2004/06/24/25872.aspx

Regular expressions, regular expressions, regular expression matching, regular expression syntax, pattern matching, regular expressions matching JavaScript regular expressions ASP regular expression ASP. NET regular Expression C # Regular expression JSP regular expression php regular expression VB. NET regular expression VBScript regular Expression programming delphi regular Expression JScript


Regular expression Regular expression
Regular Expression RegExp
Pattern patterns
Matching match
. NET namespaces: System.Text.RegularExpression




Add:
^\d+$//matching nonnegative integer (positive integer + 0)
^[0-9]*[1-9][0-9]*$//Matching positive integer
^ ((-\d+) | (0+)) $//matching non positive integer (negative integer + 0)
^-[0-9]*[1-9][0-9]*$//matching negative integers
^-?\d+$//Matching integer
^\d+ (\.\d+)? $//matching nonnegative floating-point number (positive floating-point number + 0)
^ ([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $//matching positive floating-point numbers
^ ((-\d+ (\.\d+)?) | (0+ (\.0+)) $//matching non-positive floating-point numbers (negative floating-point number + 0)
^ (-([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $///Match negative floating-point number
^ (-?\d+) (\.\d+)? $//matching floating-point number
^[a-za-z]+$//Match a string of 26 English letters
^[a-z]+$//Match a string of 26 uppercase letters
^[a-z]+$//Match string consisting of 26 lowercase letters
^[a-za-z0-9]+$//Match a string of numbers and 26 English letters
^\w+$//Match A string of numbers, 26 English letters, or underscores
^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$//matching email address
^[a-za-z]+://Match (\w+ (-\w+) *) (\. ( \



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.