Last updated on Jun 20, 2022
Name from MDN
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems. (如果你有一个问题,你想到可以用正则来解决,那么你有两个问题了。)

贪婪匹配(Greedy):默认情况下,在正则中,表示量词(Quantifiers)都是「贪婪的」,在贪婪模式下,会尝试尽可能最大长度(尽可能多)去匹配。
懒惰匹配(非贪婪,Lazy or Non-Greedy):懒惰(非贪婪)模式会尽可能短地去匹配,找出长度最小且满足要求的。
独占模式(Possessive):类似贪婪匹配,独占模式会尽可能多地去匹配,但匹配过程不会发生回溯,如果匹配失败就结束。不管是贪婪模式,还是懒惰(非贪婪)模式,都需要发生**回溯(Backreferences)**才能完成相应的功能。
| Greedy Quantifier | Lazy Quantifier | Possessive Quantifier | Description |
|---|---|---|---|
* |
*? |
*+ |
Star Quantifier: 0 or more |
+ |
+? |
++ |
Plus Quantifier: 1 or more |
? |
?? |
?+ |
Optional Quantifier: 0 or 1 |
{n} |
{n}? |
{n}+ |
Quantifier: exactly n |
{n,} |
{n,}? |
{n,}+ |
Quantifier: n or more |
{n,m} |
{n,m}? |
{n,m}+ |
Quantifier: between n and m |