大网数据广告活动
Web 标准与前端开发

URLPattern: hasRegExpGroups property

· 52CE 我爱测

The **`hasRegExpGroups`** read-only property of the URLPattern interface is a boolean indicating whether or not any of the `URLPattern` components contain regular expression capturing groups.

URL Pattern API

The hasRegExpGroups read-only property of the URLPattern interface is a boolean indicating whether or not any of the URLPattern components contain regular expression capturing groups.

You can use the hasRegExpGroups property to check if a URLPattern object is usable with certain web platform APIs which do not allow regular expression capturing groups. For example:

  • The match directive in the Use-As-Dictionary HTTP header prohibits regular expression capturing groups, as well as
  • the urlPattern condition when adding static routes using the InstallEvent.addRoutes() method.

Value

A boolean.

Examples

Using hasRegExpGroups

In the following example, a URLPattern object is used with a group delimiter containing named capturing groups called "id" and "title". The hasRegExpGroups property returns true in this case.

const pattern = new URLPattern({ pathname: "/blog/:id(\\d+){-:title}?" });
console.log(pattern.hasRegExpGroups); // true
const result = pattern.exec({ pathname: "/blog/123-some-article" });
console.log(result.pathname.groups); // {id: '123', title: 'some-article'}

It also works with anonymous capturing groups.

const pattern = new URLPattern({ pathname: "/blog/(\\d+)" });
console.log(pattern.hasRegExpGroups); // true
const result = pattern.exec({ pathname: "/blog/123" });
console.log(result.pathname.groups); // {0: '123'}

For other non-capturing groups, for example when using wildcard tokes (*), hasRegExpGroups will return false.

const pattern = new URLPattern({ pathname: "/blog/*" });
console.log(pattern.hasRegExpGroups); // false
const result = pattern.exec({ pathname: "/blog/123" });
console.log(result.pathname.groups); // {0: '123'}

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。