JavaScript Regular Expression Summary _ basics

Source: Internet
Author: User

Tools

Regexpal is an online JavaScript regular expression processor with the address: http://www.regexpal.com

Learning is important is the practice of operation, may wish to raise a chestnut:

Match number: 707-827-7019

Character Group match

[0-9] [0-9] [0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]
\d matches arbitrary Arabic numerals
\d matches any non-Arabic numerals

. Match any character

\d\d\d\d\d\d\d\d\d\d\d\d\d
\d\d\d.\d\d\d.\d\d\d\d.

Capturing grouping and post-application

Create a grouping with parentheses () to refer back to the captured grouped content with \ A

(\d) \d\1 match 707

Full match number:

^ (\ (\d{3}\) |^\d{3}[.-]? \d{3}[.-]?\d{4}$
^ Indicates line start position
(indicates the starting character of the capture group
\ (represents the opening parenthesis
\d{3} represents a matching three-digit number
\) represents a closing parenthesis
| Represents a selection
[.-]? Match an optional dot or hyphen
) captures the Terminator of a group
? Represents grouping optional
$ indicates the end of the line

Boundary

Matches the starting position of a line or string using caret ^
Match a line or a string at the end of the position using dollar character $

Quantifiers

Quantifiers are greedy by default
Greedy quantifiers first match the entire string. When trying to match, he selects as much content as possible, which is the entire input. Quantifiers match the entire character first, and if it fails, a character is rolled back again to try again. This process is called backtracking.
The lazy quantifier is to start looking for a match from the starting position of the target. Each time the string is checked for a character, look for what he wants to match. Finally, he will try to match the entire string.
The possessive word covers the entire target and then tries to find the matching content, but he tries it once and does not backtrack.
If you use. * to match any character 0 or more times

Greedy quantifier

Use curly braces {} to limit the number of times a pattern can match within a range, and, in addition, an unmodified quantifier is a greedy quantifier
7{1,} and 7+
7{0,} and 7*
7. With 7{0,1}
is essentially the same
7{m,n} will match M to N times

Lazy quantifiers

At the end, make the classifier lazy.
7?? First 7? Match 0 or 7, laziness will not match any content
7*? Match 0 7
7+? Match a 7
7{m,n}? Match M 7

About regular matching html,xml tags, write next time

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.