URLPattern:hasRegExpGroups 属性
注意:此功能在 Web Workers 中可用。
URLPattern 接口的 hasRegExpGroups 只读属性是一个布尔值,表示 URLPattern 的任何组件是否包含正则表达式捕获组。
你可以使用 hasRegExpGroups 属性来检查 URLPattern 对象是否可用于某些不允许正则表达式捕获组的 Web 平台 API。例如:
- 在
Use-As-DictionaryHTTP 标头中的match指令禁止使用正则表达式捕获组,以及 - 在使用
InstallEvent.addRoutes()方法添加静态路由时的urlPattern条件。
值
布尔值。
示例
使用 hasRegExpGroups
在下面的示例中,一个 URLPattern 对象被用于一个包含名为“id”和“title”的命名捕获组的组分隔符。在这种情况下,hasRegExpGroups 属性返回 true。
js
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'}
它也适用于匿名捕获组。
js
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'}
对于其他非捕获组,例如使用通配符标记 (*) 时,hasRegExpGroups 将返回 false。
js
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'}
规范
| 规范 |
|---|
| URL 模式 # dom-urlpattern-hasregexpgroups |
浏览器兼容性
加载中…