量词:*, +, ?, {n}, {n,}, {n,m}

量词重复 atom 一定次数。量词放置在它所适用的原子之后。

¥A quantifier repeats an atom a certain number of times. The quantifier is placed after the atom it applies to.

语法

¥Syntax

regex
// Greedy
atom?
atom*
atom+
atom{count}
atom{min,}
atom{min,max}

// Non-greedy
atom??
atom*?
atom+?
atom{count}?
atom{min,}?
atom{min,max}?

参数

¥Parameters

atom

单个 atom

count

非负整数。原子应重复的次数。

min

非负整数。原子可以重复的最小次数。

max Optional

非负整数。原子可以重复的最大次数。如果省略,原子可以根据需要重复多次。

描述

¥Description

量词放置在 atom 后面以将其重复一定次数。它不能单独出现。每个量词都能够指定模式必须重复的最小和最大数量。

¥A quantifier is placed after an atom to repeat it a certain number of times. It cannot appear on its own. Each quantifier is able to specify a minimum and maximum number that a pattern must be repeated for.

量词 最低限度 最大限度
? 0 1
* 0 无穷
+ 1 无穷
{count} count count
{min,} min 无穷
{min,max} min max

对于 {count}{min,}{min,max} 语法,数字周围不能有空格 - 否则,它会变成 literal 模式。

¥For the {count}, {min,}, and {min,max} syntaxes, there cannot be white spaces around the numbers — otherwise, it becomes a literal pattern.

js
const re = /a{1, 3}/;
re.test("aa"); // false
re.test("a{1, 3}"); // true

此行为在 Unicode 识别模式 中得到修复,如果没有 escaping,大括号就不能按字面意思出现。从字面上使用 {} 而不转义的能力是 已弃用的 Web 兼容性语法,你不应该依赖它。

¥This behavior is fixed in Unicode-aware mode, where braces cannot appear literally without escaping. The ability to use { and } literally without escaping is a deprecated syntax for web compatibility, and you should not rely on it.

js
/a{1, 3}/u; // SyntaxError: Invalid regular expression: Incomplete quantifier

如果最小值大于最大值,则为语法错误。

¥It is a syntax error if the minimum is greater than the maximum.

js
/a{3,2}/; // SyntaxError: Invalid regular expression: numbers out of order in {} quantifier

量词可以导致 捕获组 多次匹配。有关此情况下行为的更多信息,请参阅捕获组页面。

¥Quantifiers can cause capturing groups to match multiple times. See the capturing groups page for more information on the behavior in this case.

每个重复的匹配不必是相同的字符串。

¥Each repeated match doesn't have to be the same string.

js
/[ab]*/.exec("aba"); // ['aba']

默认情况下,量词是贪婪的,这意味着它们尝试尽可能多地匹配,直到达到最大值,或者直到不可能进一步匹配。你可以通过在量词后面添加 ? 来使其成为非贪婪量词。在这种情况下,量词将尝试匹配尽可能少的次数,只有在无法通过如此多次的重复来匹配模式的其余部分时才匹配更多次。

¥Quantifiers are greedy by default, which means they try to match as many times as possible until the maximum is reached, or until it's not possible to match further. You can make a quantifier non-greedy by adding a ? after it. In this case, the quantifier will try to match as few times as possible, only matching more times if it's impossible to match the rest of the pattern with this many repetitions.

js
/a*/.exec("aaa"); // ['aaa']; the entire input is consumed
/a*?/.exec("aaa"); // ['']; it's possible to consume no characters and still match successfully
/^a*?$/.exec("aaa"); // ['aaa']; it's not possible to consume fewer characters and still match successfully

但是,一旦正则表达式成功匹配某个索引处的字符串,它就不会尝试后续索引,尽管这可能会导致消耗更少的字符。

¥However, as soon as the regex successfully matches the string at some index, it will not try subsequent indices, although that may result in fewer characters being consumed.

js
/a*?$/.exec("aaa"); // ['aaa']; the match already succeeds at the first character, so the regex never attempts to start matching at the second character

如果不可能匹配模式的其余部分,贪婪量词可能会尝试更少的重复。

¥Greedy quantifiers may try fewer repetitions if it's otherwise impossible to match the rest of the pattern.

js
/[ab]+[abc]c/.exec("abbc"); // ['abbc']

在此示例中,[ab]+ 首先贪婪地匹配 "abb",但 [abc]c 无法匹配模式的其余部分 ("c"),因此量词被简化为仅匹配 "ab"

¥In this example, [ab]+ first greedily matches "abb", but [abc]c is not able to match the rest of the pattern ("c"), so the quantifier is reduced to match only "ab".

贪婪量词避免匹配无限多个空字符串。如果达到最小匹配数并且该位置的原子不再消耗任何字符,则量词停止匹配。这就是 /(a*)*/.exec("b") 不会导致无限循环的原因。

¥Greedy quantifiers avoid matching infinitely many empty strings. If the minimum number of matches is reached and no more characters are being consumed by the atom at this position, the quantifier stops matching. This is why /(a*)*/.exec("b") does not result in an infinite loop.

贪婪量词尝试尽可能多地匹配;它不会最大化比赛的长度。例如,/(aa|aabaac|ba)*/.exec("aabaac") 匹配 "aa",然后匹配 "ba" 而不是 "aabaac"

¥Greedy quantifiers try to match as many times as possible; it does not maximize the length of the match. For example, /(aa|aabaac|ba)*/.exec("aabaac") matches "aa" and then "ba" instead of "aabaac".

量词适用于单个原子。如果你想量化较长的模式或析取,则必须对其进行 group。量词不能应用于 assertions

¥Quantifiers apply to a single atom. If you want to quantify a longer pattern or a disjunction, you must group it. Quantifiers cannot be applied to assertions.

js
/^*/; // SyntaxError: Invalid regular expression: nothing to repeat

Unicode 识别模式前瞻断言 中可以量化。这是 已弃用的 Web 兼容性语法,你不应该依赖它。

¥In Unicode-aware mode, lookahead assertions can be quantified. This is a deprecated syntax for web compatibility, and you should not rely on it.

js
/(?=a)?b/.test("b"); // true; the lookahead is matched 0 time

示例

¥Examples

删除 HTML 标签

¥Removing HTML tags

以下示例删除尖括号内的 HTML 标记。请注意使用 ? 以避免一次消耗太多字符。

¥The following example removes HTML tags enclosed in angle brackets. Note the use of ? to avoid consuming too many characters at once.

js
function stripTags(str) {
  return str.replace(/<.+?>/g, "");
}

stripTags("<p><em>lorem</em> <strong>ipsum</strong></p>"); // 'lorem ipsum'

使用贪心匹配可以达到相同的效果,但不允许重复模式匹配 >

¥The same effect can be achieved with a greedy match, but not allowing the repeated pattern to match >.

js
function stripTags(str) {
  return str.replace(/<[^>]+>/g, "");
}

stripTags("<p><em>lorem</em> <strong>ipsum</strong></p>"); // 'lorem ipsum'

警告:这仅用于演示 - 它不处理属性值中的 >。请改用适当的 HTML 清理程序,例如 HTML 清理 API

¥Warning: This is for demonstration only — it doesn't handle > in attribute values. Use a proper HTML sanitizer like the HTML sanitizer API instead.

定位 Markdown 段落

¥Locating Markdown paragraphs

在 Markdown 中,段落由一个或多个空行分隔。以下示例通过匹配两个或多个换行符来计算字符串中的所有段落。

¥In Markdown, paragraphs are separated by one or more blank lines. The following example counts all paragraphs in a string by matching two or more line breaks.

js
function countParagraphs(str) {
  return str.match(/(?:\r?\n){2,}/g).length + 1;
}

countParagraphs(`
Paragraph 1

Paragraph 2
Containing some line breaks, but still the same paragraph

Another paragraph
`); // 3

警告:这仅用于演示 - 它不处理代码块或其他 Markdown 块元素(如标题)中的换行符。请改用适当的 Markdown 解析器。

¥Warning: This is for demonstration only — it doesn't handle line breaks in code blocks or other Markdown block elements like headings. Use a proper Markdown parser instead.

规范

Specification
ECMAScript Language Specification
# prod-Quantifier

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看