Home > Article > Web Front-end > In-depth understanding of the analysis of quantifiers in JS regular expressions
This article mainly introduces the analysis of quantifiers for in-depth understanding of JS regular expressions. It has a certain reference value. Now I share it with you. Friends in need can refer to it
Many times, we need to match a string with characters that appear many times in a row. For example, we need to match a string with a number that appears 20 times in a row. According to the previous writing:
\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d
Does it feel fast? crazy? Fortunately, using the quantifiers provided by regular expressions, we can quickly solve this problem. The usage of
quantifiers is as follows:
Characters | Meaning |
---|---|
? | Occurs zero or once (occurs at most once) |
Occurs one or more times (occurs at least once) | |
* | Occurs zero or more times (any number of times) |
{n} | Occurs n times |
{n,m} | occurs n to m times |
{n,} | Occurs at least n times |
#So how to use quantifiers to solve the previous problem?
It's very simple, just need:
\d{20}
What should you do if you want to match up to n times? Many people will want to imitate it appearing at least n times and write it as {,n}
, but the regular expression will directly treat it as an ordinary string. Only by writing it as {0, n}
can match at most n times.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
In-depth understanding of JS Analysis of range classes of regular expressions
The above is the detailed content of In-depth understanding of the analysis of quantifiers in JS regular expressions. For more information, please follow other related articles on the PHP Chinese website!