このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

URLPattern: hasRegExpGroups プロパティ

Baseline 2025
Newly available

Since September 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

メモ: この機能はウェブワーカー内で利用可能です。

hasRegExpGroupsURLPattern インターフェイスの読み取り専用プロパティで、URLPattern 要素のいずれかが 正規表現のキャプチャグループ を含むかどうかを示す論理値です。

hasRegExpGroups プロパティを使用すると、正規表現のキャプチャグループを許可しない特定のウェブプラットフォーム API で URLPattern オブジェクトが使用可能かどうかを確認できます。例えば、

  • match ディレクティブは、HTTP の Use-As-Dictionary ヘッダーにおいて、正規表現のキャプチャグループを禁止します。
  • urlPattern 条件は、InstallEvent.addRoutes() メソッドを使用して静的ルートを追加する際に適用されます。

論理値です。

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'}

その他の非キャプチャグループの場合、例えばワイルドカードトークン (*) を使用する場合、hasRegExpGroupsfalse を返します。

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'}

仕様書

Specification
URL Pattern
# dom-urlpattern-hasregexpgroups

ブラウザーの互換性