Favorite Regular Expression Encyclopedia _ regular expression

Source: Internet
Author: User
Tags character set html tags lowercase trim

Regular expressions, also known as formal representations, general representations. (English: Regular Expression, often abbreviated as regex, RegExp, or re) in code, a concept of computer science. A regular expression uses a single string to describe and match a sequence of rules that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.

I. Classification

1. "^" Homogeneous

"^" matches the start position of the input string.
"$" matches the end position of the input string.
"\b" matches a word boundary, which refers to the position between words and spaces. (For example: "er\b" can Match "er" in "never", but cannot match "er" in "verb")
"\b" and "\b", in contrast, match a non-word boundary (for example: "er\b" can Match "er" in "verb", but cannot match "er" in "Never")

2. "*" Similar

Asterisk (*): The asterisk represents a character that matches one of its previous characters, any time (0 or any).
plus sign (+): Indicates that the preceding character is matched one or more times (at least once).
Question mark (?): A question mark is also a quantity word that represents 0 or 1 occurrences of the preceding character.
Bracket []: The brackets are used to denote a character set combination, if this collection has many elements, such as 26 letters, numbers, and so on, written in brackets, it would be too much trouble, so generally we use hyphens to denote a range (for example: [A-z] a set of lowercase letters; [a-za-z] Represents a collection of uppercase and lowercase letters. The caret "^" indicates that any characters that are not in the collection are matched. (For example: [^a-z]).
Curly braces {}: The function of curly braces is to repeat the previous character many times (for example: {n}: repeat n times; {n,m}: repeat n~m; {N,}: Repeat at least n times; {, m}: Max repeat m)

3. "\s" of the same Kind

"\w" (lowercase w) denotes letters or numbers, equivalent to [a-za-z0-9]
"\w" (capital W) is not a letter and is not a number, as opposed to \w, equivalent to [^a-za-z0-9]
"\s" (lowercase s) indicates matching an empty character, including spaces, wrapping, carriage return, tab, equivalent to [\n\r\t\f]
"\s" (capital S) matches a non-whitespace character, which, contrary to \s, is equivalent to [^ \n\r\t\f]
"\d" (lowercase D) represents a decimal digit, equivalent to [0-9]
"\d" (capital D) matches a non-numeric character, equivalent to [^0-9]

Second, declare a checknum function

function Checknum (input) {/
 * indicates that it must be 1 or more digits */
 var regex =/^[a-z]{6,12}$/;
 if (Input.match (regex)) {return
 true;
 } else {return
 false;
}} 

Three, declare a checkemail function

function Checkemail (input) {
 var regex =/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/;
 if (Input.match (regex)) {return
  true;
 } else {return
  false;
}}}

Four, Package a trim ()

var my = function () {};
My.prototype = {
 ltrim:function (str) {return
 str.replace (/(^\s*)/g, ');
 },
 rtrim:function (str) { return
 str.replace (/(\s*$)/g, ");
 },
 trim:function (str) {return
 str.replace (/(^\s*) | ( \s*$)/g, ');
 }
 /*^ to xx opening/
 /*\s said the space * * *
 /** to match 0 or more * *
 /*g to match all *
 /* (^\s*) to match the beginning of one or more characters with a space//////
 * Str.replace (/(^\s*)/g, "") means "replace all spaces".

attached: Common Regular Expression of the encyclopedia! (For example: matching Chinese, matching HTML)

Matching regular expressions for Chinese characters: [U4E00-U9FA5]
Commentary: Matching Chinese is really a headache, with this expression will be easy to do
Match Double-byte characters (including Chinese characters): [^x00-xff]
Commentary: can be used to compute the length of a string (a double-byte character length meter 2,ascii 1 characters)
A regular expression that matches a blank row: ns*r
Commentary: can be used to delete blank lines
Regular expression:< matching HTML tags (s*?) [^>]*>.*?| <.*? />
Commentary: The online version is too bad, the above can only match the part of the complex nested tags still powerless
A regular expression that matches the end-end whitespace character: ^s*|s*$
Commentary: A useful expression that can be used to delete white-space characters (including spaces, tabs, page breaks, and so on) at the end of a line at the beginning
Regular expression matching an email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *
Commentary: Form validation is useful
Regular expressions that match URL URLs: [a-za-z]+://[^s]*
Commentary: Online circulation of the version of the function is very limited, which can meet the basic requirements
Match account number is legal (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$
Commentary: Form validation is useful
Match domestic phone number: D{3}-d{8}|d{4}-d{7}
Commentary: Match form such as 0511-4405222 or 021-87888822
Matching Tencent QQ Number: [1-9][0-9]{4,}
Commentary: Tencent QQ number starting from 10000
Match China ZIP Code: [1-9]d{5} (?! D
Commentary: China postal code is 6 digits
Matching ID: d{15}|d{18}
Commentary: China's ID card is 15-or 18-digit
Matching IP address: d+.d+.d+.d+
Commentary: Useful when extracting IP addresses

Match a specific number:

^[1-9]d*$//Matching positive integer
^-[1-9]d*$//matching negative integers
^-? [1-9]d*$//matching integer
^[1-9]d*|0$//matching nonnegative integer (positive integer + 0)
^-[1-9]d*|0$//matching non positive integer (negative integer + 0)
^[1-9]d*.d*|0.d*[1-9]d*$//matching positive floating-point numbers
^-([1-9]d*.d*|0.d*[1-9]d*) $//matching negative floating-point number
^-? ([1-9]d*.d*|0.d*[1-9]d*|0?. 0+|0) $//matching floating-point number
^[1-9]d*.d*|0.d*[1-9]d*|0? 0+|0$//matching nonnegative floating-point number (positive floating-point number + 0)
^ (-([1-9]d*.d*|0.d*[1-9]d*)) |? 0+|0$//matching non-positive floating-point numbers (negative floating-point number + 0)
Commentary: useful when dealing with large amounts of data, pay attention to corrections when applied

Match a specific string:

^[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

Only numbers can be entered: "^[0-9]*$"
can only enter n digits: "^d{n}$"
can only enter at least n digits: "^d{n,}$"
can only enter the number of m-n digits: "^d{m,n}$"
can only enter numbers beginning with 0 and 0: "^ (0| [1-9] [0-9]*) $ '
only positive real numbers with two decimal digits are entered: ^[0-9]+ (. [ 0-9]{2})? $
You can only enter positive real numbers with 1-3 decimal digits: ^[0-9]+ (. [ 0-9]{1,3})? $
You can only enter a Non-zero positive integer: ^+?[ 1-9][0-9]*$
can only enter a Non-zero negative integer: "^-[1-9][0-9]*$"
can only enter characters with a length of 3: "^.{ 3}$
You can only enter a string of 26 English letters: "^[a-za-z]+$"
can only enter a string of 26 uppercase letters: "^[a-z]+$"
can only enter a string of 26 lowercase English letters: "^[a-z]+$" The
can only enter a string of numbers and 26 English letters: "^[a-za-z0-9]+$"
can only enter a string of numbers, 26 English letters, or underscores: "^w+$"

Verify user password: "^[a-za-z]w{5,17}$" in the correct format: Beginning with the letter, length between 6-18,
Only characters, numbers, and underscores can be included.
Verify that there are ^%& ',; =?$ ' characters: "[^%&",; = $x 22]+ "
Only Chinese characters can be entered: "^[u4e00-u9fa5],{0,}$"
Verify email Address: "^w+[-+." w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *$ "
Verify InternetURL: "^http://([w-]+.) +[w-]+ (/[w-./?%&=]*)? $ "
Verify phone Number: "^ ((d{3,4}) |d{3,4}-)? d{7,8}$"
The correct format is: "Xxxx-xxxxxxx", "xxxx-xxxxxxxx", "xxx-xxxxxxx",
"Xxx-xxxxxxxx", "XXXXXXX", "XXXXXXXX".
Verify ID Number (15-bit or 18-digit): "^d{15}|d{}18$"
Verify 12 months of the year: "^" (0?[ 1-9]|1[0-2]) $ "The correct format is:" 01 "-" 09 "and" 1 "" 12 "
Verify one months of 31 days: "^ (0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ "
The correct format is: "01" "09" and "1" "31".
Matching regular expressions for Chinese characters: [U4E00-U9FA5]
Match Double-byte characters (including Chinese characters): [^x00-xff]
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*$)
Regular expression matching an email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *
A regular expression that matches URL URLs: http://([w-]+.) +[w-]+ (/[w-/?%&=]*)?

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.