The basic knowledge of JS regular expression

Source: Internet
Author: User
Tags expression regular expression

1, "." is a wildcard that represents any one character, for example: "A.C" can match "ANC", "ABC", "ACC";

2, "[]", in [] can specify the required matching characters, such as: "A[nbc]c" can match "ANC", "ABC", "ACC"

But can not match "ANCC", A to Z can be written [a-z],0 to 9 can be written [0-9];

3, Quantity qualifying symbol, indicating the number of matches (or length) of the symbol:

Includes: "*"--0 or more times

"+"--1 or more times

“?” --0 or 1 times

' {n} '--match n times, N is an integer

' {n,m} '--the number of occurrences of a number from N to M; n and m are integers;

"{N,}"--matches any number of times between N and Infinity;

"{, M}"--matches any number of times between 0 and M;

They are placed behind the matching format:

For example:

Phone Number: 024-84820482,02484820482 (assuming the front 3 or 4 digits, the back 7 or 8 digits, and the middle minus is optional)

are compliant, they can be matched in the following format: [0-9]{3,4}-? [0-9] {7,8};

Note: "" is an escape character because "-" is used in a regular expression to represent a range of meanings, for example: [0-9] as described above,

So it needs the escape character "" to be escaped to use;

4, "^" is no symbol, indicating do not want to match the symbol, for example: [^z][a-z]+ can match all except the beginning of "Z" all words

String (length greater than 2, because "+" means the number of times greater than or equal to 1, starting from the second is lowercase English characters);

If ^ is placed outside [] a string that begins with []; ^[az][a-z]+ represents an English character with a length greater than or equal to 2 at the beginning of a or Z

String

5, "|" Or operators, such as: A[n|bc|cb]c can Match "ABCC", "ANC", "ACBC";

6, "$" at the end of the character before it; for example: ab+$ can be "ABB", "AB" match;

7. Some simple ways to express:

D means [0-9];D Express [^0-9];W [a-z0-9]; W indicates [^a-z0-9];s [TNRF], which is the space character including tab

, spaces and so on; s represents [^TNRF], is not a space character;

8. Common matching:

Match Chinese characters: "[U4e00-u9fa5]";

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

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

A regular expression that matches an HTML tag: "/.*|/";

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

Match nonnegative integer (positive integer + 0): "^d+$";

Match positive integer: "^[0-9]*[1-9][0-9]*$";

Match a non positive integer (negative integer + 0): "^ ((-d+) | ( 0+)) $ ";

Match negative integer: "^-[0-9]*[1-9][0-9]*$";

Match integer: "^-?d+$";

matching nonnegative floating-point numbers (positive floating-point number + 0): "^d+ (. d+)? $"

Matching positive floating-point number: "^ ([0-9]+.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*. [0-9]+) | ([0-9]*[1-9][0-9]*

)) $ ";

^ ((-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 numbers

Matching floating-point number: "^ (-?d+) (. d+)? $";

Match a string of numbers, 26 English letters, or underscores: "^w+$";

Match email address: "^[w-]+. [ w-]+) *@[w-]+ (. [ w-]+) +$ ";

Match URL: "^[a-za-z]+://match (w+ (-w+) *) (. w+ (-w+) *) *) * (? s*)? $ "

1, "." is a wildcard that represents any one character, for example: "A.C" can match "ANC", "ABC", "ACC";
2, "[]", in [] can specify the required matching characters, such as: "A[nbc]c" can match "ANC", "ABC", "ACC"
But can not match "ANCC", A to Z can be written [a-z],0 to 9 can be written [0-9];

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.