Usage of JavaScript Regular expressions

Source: Internet
Author: User
Tags html tags lowercase

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: 0755-12345678,075512345678 (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 "Z" of all the strings (length greater than 2, because "+" is greater than or equal to 1 of the number of times, starting from the second is lowercase English characters);
If ^ is placed outside [], it represents a string beginning with []; ^[az][a-z]+ represents an English string with a length greater than or equal to 2 at the beginning of A or Z;

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 said [^a-z0-9];s [TNRF], is the space character including tab, space 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 ";
Regular expression matching HTML tags: "/< (. *) >.*</1>|< (. *)/>/";
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 number
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*)? $ "


Some common regular expressions

<strong>//Matching Email address </strong>
w+ ([-+. '] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *
Match Chinese characters
[U4e00-u9fa5]
<strong>//match 15-bit or 18-bit ID </strong>
(^d{15}$/) | (D{17} (?:d |x| X) $
<strong>//Matching IP address </strong>
D+.d+.d+.d+
Match Blank Line
Ns*r//Can be used to delete blank lines
Match Double-byte characters (including Chinese characters)
[^x00-xff]
Match the trailing whitespace characters
^s*|s*$
<strong>//matching domestic phone number </strong>
D{3}-D{8}|D{4}-D{7}///commentary: match form such as 0511-4405222 or 021-87888822
<strong>//Matching mobile number </strong>
/^1 (3|5|8) d{9}$/
Match img Tags
&AMP;LT;IMG[^&AMP;GT;] +&gt;
Match img Address
&lt;img.+?src= ([' "]?) ([^&gt;s]+) 1.*?&gt;
Matching HTML tags
&lt; (s*?) [^&gt;] *&gt;. *?&lt;/1&gt;|&lt;. *? /&gt;
<strong>//Matching URL URLs </strong>
(FTP|HTTP|HTTPS)://(w+:{0,1}w*@)? (s+) (: [0-9]+)? (/|/([w#!:.? +=&amp;%@!-/])?
<strong>//matching Tencent QQ number:</strong>
[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
Match a specific number:
^d+$//matching numbers
^[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)
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
^[a-za-z]w{5,17}$//starts with a letter, length between 6~18, can only contain characters, numbers, and underscores

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.