# 限定符

限定符也指数量词（Quantifiers），用来指定正则表达式的一个给定模式必须要出现多少次才能满足匹配

## 限定符集

| 字符                   | 描述                                                                                                                                                                                     |
| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \*                   | <ul><li>匹配前面的子表达式零次或多次</li><li>例如，zo\* 能匹配 "z" 以及 "zoo"，\* 等价于{0,}</li></ul>                                                                                                           |
| +                    | <ul><li>匹配前面的子表达式一次或多次</li><li>例如，'zo+' 能匹配 "zo" 以及 "zoo"，+ 等价于 {1,}</li></ul>                                                                                                         |
| ?                    | <ul><li>匹配前面的子表达式零次或一次</li><li>例如，"do(es)?" 可以匹配 "do" 、 "does" ，? 等价于 {0,1}</li></ul>                                                                                                  |
| <p>\*? </p><p>+?</p> | <ul><li>像 \* 和 + 一样匹配前面的模式，但匹配是最小可能匹配</li><li>例如，/".\*?"/<em>匹配 '"foo" "bar"' 中的 '"foo"'</em></li><li>后面没有 ? 时匹配 '"foo" "bar"'</li></ul>                                               |
| x(?=y)               | <ul><li>只有当<em><code>x</code></em>后面紧跟着 <em><code>y</code></em> 时，才匹配 <em><code>x</code></em></li><li>例如，<code>/x(?=y)/</code>只有在 'x' 后面紧跟着 'y' 时，才会匹配它</li><li>但y不是匹配结果的一部分</li></ul> |
| x(?!y)               | <ul><li>只有当 <em><code>x</code></em> 后面不是紧跟着 <em><code>y</code></em> 时，才匹配 <em><code>x</code></em></li><li>例如，<code>/\d+(?!.)/.exec("3.141")</code> 匹配 141 而不是 3.141</li></ul>          |
| {n}                  | <ul><li>n 是一个非负整数</li><li>匹配确定的 n 次</li><li>例如，'o{2}' 不能匹配 "Bob" 中的 'o'，匹配 "food" 中的两个 o</li></ul>                                                                                     |
| {n,}                 | <ul><li>n 是一个非负整数</li><li>至少匹配n 次</li><li>例如，'o{2,}' 不能匹配 "Bob" 中的 'o'，匹配 "foooood" 中的所有 o</li><li>'o{1,}' 等价于 'o+'，'o{0,}' 则等价于 'o\*'</li></ul>                                       |
| {n,m}                | <ul><li>m 和 n 均为非负整数，其中n <= m</li><li>最少匹配 n 次且最多匹配 m 次</li><li>例如，"o{1,3}" 将匹配 "fooooood" 中的前三个 o</li><li>'o{0,1}' 等价于 'o?'</li><li>注意在逗号和两个数之间不能有空格</li></ul>                        |

## 限定符模式

### 贪婪模式

> 贪婪模式，指在整个表达式匹配成功的前提下，尽可能多的匹配；贪婪模式的量词："{m,n}"，"{m,}"，"?"，"\*"和"+"

* 匹配o子表达式零次或多次

```javascript
/zo*/.test('zooooooo'); // true
/zo{0,}/.test('zooooooo'); // true

/zo*/.test('zmooooo'); // true
/zo{0,}/.test('zooooooo'); // true
```

![](/files/-LivPVp4J3qI-CIv5QXb)

* 匹配o子表达式一次或多次

```javascript
/zo+/.test('zooooooo'); // true
/zo{1,}/.test('zooooooo'); // true

/zo+/.test('zmooooo'); // false
/zo{1,}/.test('zmooooo'); // false
```

![](/files/-LivPbkK93qA04pTOFup)

* 匹配o子表达式零次或一次

```javascript
/zo?/.test('zooooooo'); // true
/zo{0,1}/.test('zooooooo'); // true

/zo?/.test('zmooooo'); // true
/zo{0,1}/.test('zmooooo'); // true
```

![](/files/-LivPivKe-bTskoK0syO)

### 非贪婪模式（最小可能匹配）

> 非贪婪模式，指在整个表达式匹配成功的前提下，尽可能少的匹配；在贪婪模式量词后加上“?”，即变成非贪婪模式的量词："{m,n}?"，"{m,}?"，"??"，"\*?"和"+?"

```javascript
// ??和{0,1}? 匹配零个o
/zo??/.exec("zooooo"); // ["z", index: 0, input: "zooooo", groups: undefined]
/zo{0,1}?/.exec("zooooo"); // ["z", index: 0, input: "zooooo", groups: undefined]
```

![](/files/-LivQA8A8x9QhFCAH30I)

```javascript
// *?和{0,} 匹配零个o
/zo*?/.exec("zooooo"); // ["z", index: 0, input: "zooooo", groups: undefined]
/zo{0,}?/.exec("zooooo"); // ["z", index: 0, input: "zooooo", groups: undefined]
```

![](/files/-LivQJTajsg7nr2v1WSr)

```javascript
// +?和{1,}? 匹配一个o
/zo+?/.exec("zooooo"); // ["zo", index: 0, input: "zooooo", groups: undefined]
/zo{1,}?/.exec("zooooo"); // ["zo", index: 0, input: "zooooo", groups: undefined]
```

![](/files/-LivQU_eyEgXiiibkhIE)

### 判断匹配模式

> 判断匹配模式指，某个子表达式后是否存在或不存在某个模式，进而进行匹配

* x(?=y) x表达式后存在y表达式

```javascript
/一起学习(?=RegExp)/.test("一起学习RegExp"); // true
/一起学习(?=RegExp)/.test("一起学习React"); // false
```

![](/files/-LivQbqNxp1pxkpfqT0V)

* x(?!y) x表达式后不存在y表达式

```javascript
/一起学习(?!RegExp)/.test("一起学习React"); // true
/一起学习(?!RegExp)/.test("一起学习RegExp"); // false
```

![](/files/-LivR9VO88YHn29eOw-Y)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nxtech.gitbook.io/regexp/grammar/quantifiers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
