Home > Web Front-end > JS Tutorial > JavaScript regular expression summary_basic knowledge

JavaScript regular expression summary_basic knowledge

WBOY
Release: 2016-05-16 15:13:05
Original
1598 people have browsed it

Tools

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

The most important thing in learning regular rules is practical operation. Let’s give an example:

Matching Number: 707-827-7019

Character group matching

[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9] [0-9]
d matches any Arabic digit
D matches any non-Arabic digit

. Matches any character

dddDdddDddddD
ddd.ddd.dddd.

Capture grouping and backward application

Use parentheses () to create a group, and use 1 to back reference the captured group content

(d)d1 then matches 707

Full matching number:

^((d{3})|^d{3}[.-]?)?d{3}[.-]?d{4}$
^ represents the starting position of a line
(Indicates the start character of the capture group
( represents the left bracket
d{3} means matching three digits
) represents the right bracket
| indicates selection
[.-]? matches an optional period or hyphen
) Capture the end character of the group
? Indicates that grouping is optional
$ means until the end of the line

Border

Use the caret ^ at the beginning of a matching line or string
Use the dollar sign $

to match the end of a line or string

Quantifier

Quantifiers are greedy by default
A greedy quantifier will match the entire string first. When trying to match, it selects as much as possible, which is the entire input. The quantifier first matches the entire character, and if it fails, falls back one character and tries again. This process is called backtracking.
The lazy quantifier searches for matches starting from the starting position of the target. Checks the string one character at a time, looking for what it matches. Finally, it will try to match the entire string.
The possessive quantifier will cover the entire target and then try to find a match, but it will only try once and will not backtrack.
If you use .* to match any character zero or more times

Greedy quantifier

Use curly braces {} to limit the number of times a pattern is matched within a certain range. In addition, unmodified quantifiers are greedy quantifiers
7{1,} and 7+
7{0,} and 7*
7? and 7{0,1}
Essentially it’s the same
7{m,n} will match m to n times

Lazy quantifier

Adding ? at the end makes the quantifier lazy
7?? First 7? matches zero or one 7, after laziness it will not match anything
7*? matches zero 7
7+? matches a 7
7{m,n}? Match m 7

Regarding regular matching of html and xml tags, I will write about it next time

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template