Basic js Regular Expressions

Source: Internet
Author: User

1. "." is a wildcard that represents any character. For example, "a. c" can match "anc", "abc", and "acc ";

2. "[]", you can specify the matching characters in []. For example, "a [nbc] c" can match "anc", "abc", and "acc"

But it cannot match "ancc". a to z can be written as [a-z], 0 to 9 can be written as [0-9];

3. quantity limit symbol, indicating the number of matching times (or length:

Including: "*" -- 0 or multiple times

"+" -- Once or multiple times

"?" -- 0 or 1 time

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

"{N, m}" -- number of times a number is matched from n to m; n and m are integers;

"{N ,}" -- match any number of times between n and infinity;

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

They are placed behind the matching format:

For example:

Phone number: 024-84820482,02484820482 (assume that the first 3 or 4 digits are followed by 7 or 8 digits, and the minus sign in the middle is optional)

All are compliant, so you can use the following format to match: [0-9] {3, 4 }-? [0-9] {7,8 };

Note: "" is an escape character, because "-" represents a range in a regular expression, for example, [0-9],

Therefore, the escape character "" is required for conversion;

4. "^" indicates no symbols that do not match. For example, [^ z] [a-z] + can match all characters starting with "z ".

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

If ^ is placed outside [], it indicates a string starting with []. ^ [az] [a-z] + indicates an English character whose length starts with a or z is greater than or equal to 2.

String;

5. "|" or operator. For example, a [n | bc | cb] c can match "abcc", "anc", and "acbc ";

6. "$" ends with the character above it. For example, AB + $ can be matched by "abb" and "AB;

7. Some Simple Representation Methods:

D Represents [0-9]; D represents [^ 0-9]; w represents [A-Z0-9]; W represents [^ A-Z0-9]; s represents [tnrf], that is, the space character includes tab

, Space, etc.; S indicates [^ tnrf], which is a non-space character;

8. Common matching:

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

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

Match the Regular Expression of null rows: "n [s |] * r ";

Match the regular expression marked in HTML: "/. * | /";

Regular Expression matching the first and last spaces: "(^ s *) | (s * $ )";

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

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

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

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

Matched INTEGER: "^ -? D + $ ";

Match non-negative floating point number (Positive floating point number + 0): "^ d + (. d + )? $"

Match 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 + )?)) $ // 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] *) $ // match

Negative floating point number

Match floating point: "^ (-? D +) (. d + )? $ ";

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

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

Match url: "^ [a-zA-z] +: // match (w + (-w + )*)(. (w + (-w + )*))*(? S *)? $"

1. "." is a wildcard that represents any character. For example, "a. c" can match "anc", "abc", and "acc ";
2. "[]", you can specify the matching characters in []. For example, "a [nbc] c" can match "anc", "abc", and "acc"
But it cannot match "ancc". a to z can be written as [a-z], 0 to 9 can be written as [0-9];

Related Article

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.