Javascript Regular Expression summary, Regular Expression

Source: Internet
Author: User

Javascript Regular Expression summary, Regular Expression

Tools

Regexpal is an online Javascript Regular Expression processor with address: http://www.regexpal.com

Learning regular expressions is important to practice. For example:

Matched number: 707-827-7019

Character group match

[0-9] [0-9] [0-9]-[0-9] [0-9] [0-9]-[0-9] [0-9- 9] [0-9] [0-9]
\ D match any Arabic numerals
\ D match any non-Arabic number

. Match any character

\ D \ D \ d
\ D.

Capture groups and backward applications

Use parentheses () to create a group and use \ 1 to reference the captured group content.

(\ D) \ d \ 1 matches 707

Complete matching number:

^ (\ D {3} \) | ^ \ d {3} [.-]?)? \ D {3} [.-]? \ D {4} $
^ Indicates the starting position of a row
(Indicates the start character of the capture group.
\ (Left parenthesis
\ D {3} indicates matching three digits
\) Represents the right parenthesis
| Indicates Selection
[.-]? Match an optional dot or hyphen
) Capture group Terminator
? Indicates that the group is optional.
$ Indicates that the row ends.

Boundary

Use the escape character to match the starting position of a line or string ^
Use the dollar sign to match the end of a row or string $

Quantifiers

The quantifiers are greedy by default.
Greedy quantifiers match the entire string first. When a match is attempted, the system selects as much content as possible, that is, the entire input. The quantifiers match the entire character first. If it fails, a character will be rolled back and tried again. This process is called backtracking.
Lazy quantifiers start from the starting position of the target to search for matching. Each time you check a character in a string, you can find the matching content. Finally, he will try to match the entire string.
A placeholder quantizer will overwrite the entire target and then try to find the matching content. However, it only tries once and does not backtrack.
If you use. * to match any character Zero or multiple times

Greedy quantifiers

You can use curly braces {} to limit the number of times a pattern matches within a certain range. In addition, the unmodified quantifiers are greedy quantifiers.
7 {1,} and 7 +
7 {0,} and 7 *
7? And 7 {0, 1}
Essentially the same
7 {m, n} matches m to n times

Lazy Quantizer

ADD? The quantifiers become lazy.
7 ?? First 7? Matches zero or one 7, and will not match any content after laziness
7 *? Match zero 7
7 +? Match A 7
7 {m, n }? Match m 7

For regular expression matching html and xml tags, write them again next time.

Articles you may be interested in:
  • Detailed explanation of the js Code of the regular expression in the verified email address
  • Practical JS Regular Expressions (mobile phone number/IP address Regular Expression/zip code Regular Expression/phone number)
  • Use of js Regular Expressions
  • JS Regular Expressions (detailed and practical)
  • Verify the digital code using the JS Regular Expression
  • Differences between the test, exec, and match methods in js Regular Expressions
  • Js regular expressions match numbers, letters, underscores, and so on
  • Basic Syntax of js Regular Expressions (essence)
  • JavaScript Regular Expression to verify the validity of the ID card number (two methods)
  • 12 common js Regular Expressions
  • Tips for parsing URLs using JavaScript Regular Expressions

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.