URLPattern:hasRegExpGroups 屬性

基準線 2025
新推出

自 ⁨2025 年 9 月⁩起,此功能適用於最新裝置和瀏覽器版本。此功能可能不適用於較舊的裝置或瀏覽器。

注意:此功能在 Web Workers 中可用。

URLPattern 介面的 hasRegExpGroups 只讀屬性是一個布林值,表示 URLPattern 的任何元件是否包含正則表示式捕獲組

你可以使用 hasRegExpGroups 屬性來檢查 URLPattern 物件是否可用於某些不允許正則表示式捕獲組的 Web 平臺 API。例如:

布林值。

示例

使用 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

瀏覽器相容性