Regular Expression example

Source: Internet
Author: User

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

Regular Expression matching spaces at the beginning and end: (^/S *) | (/S * $)

Match the regular expression of the email address:/W + ([-+.] /W +) * @/W + ([-.] /W + )*/. /W + ([-.] /W + )*

The regular expression matching the URL: http: // ([/W-] +/.) + [/W-] + (/[/W -./? % & =] *)?

/(/D +)/. (/d +)/g // match the regular expression of the IP address

The following describes how to use a regular expression to match an IP address and convert the IP address to a corresponding value in Javascript.Program:

Function ip2v (IP)
{
Re =/(/d +)/. (/d +)/g // The regular expression that matches the IP address
If (Re. Test (IP ))
{
Return Regexp. $1 * Math. Pow (255) + Regexp. $2 * Math. Pow () + Regexp. $3 * + Regexp. $4*1
}
Else
{
Throw new error ("not a valid IP address! ")
}
}

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 the full-width characters: onkeyup = "value = value. replace (/[^/uff00-/Uffff]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^/uff00-/Uffff]/g ,''))"

Use a regular expression to limit that only numbers can be entered: 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 ,''))"

^/D + $ // match a non-negative integer (positive integer + 0)
^ [0-9] * [1-9] [0-9] * $ // match a positive integer
^ (-/D +) | (0 +) $ // match a non-positive integer (negative integer + 0)
^-[0-9] * [1-9] [0-9] * $ // match a negative integer
^ -? /D + $ // match the integer
^/D + (/./d + )? $ // Match non-negative 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] *) $ // matches the Positive floating point number
^ (-/D + (/./d + )?) | (0 + (/. 0 + )?)) $ // Match a non-Positive floating point number (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] *) $ // matches a negative floating point number.
^ (-? /D +) (/./d + )? $ // Match floating point numbers
^ [A-Za-Z] + $ // match a string consisting of 26 English letters
^ [A-Z] + $ // match a string consisting of 26 uppercase letters
^ [A-Z] + $ // match a string consisting of 26 lowercase letters
^ [A-Za-z0-9] + $ // match a string consisting of digits and 26 letters
^/W + $ // match a string consisting of digits, 26 English letters, or underscores
^ [/W-] + (/. [/W-] +) * @ [/W-] + (/. [/W-] +) + $ // match the email address
^ [A-Za-Z] +: // match (/W + (-/W + )*)(/. (/W + (-/W + )*))*(/? /S *)? $ // Match the URL


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.