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

URLPattern

· 52CE 我爱测

The **`URLPattern`** interface of the URL Pattern API matches URLs or parts of URLs against a pattern. The pattern can contain capturing groups that extract parts of the matched URL.

URL Pattern API

The URLPattern interface of the URL Pattern API matches URLs or parts of URLs against a pattern. The pattern can contain capturing groups that extract parts of the matched URL.

More information about the syntax of patterns can be found on the API overview page: URL Pattern API.

Constructor

  • URLPattern.URLPattern
    • : Returns a new URLPattern object based on the given pattern and base URL.

Instance properties

  • URLPattern.hash
    • : A string containing a pattern to match the hash part of a URL.
  • URLPattern.hasRegExpGroups
  • URLPattern.hostname
    • : A string containing a pattern to match the hostname part of a URL.
  • URLPattern.password
    • : A string containing a pattern to match the password part of a URL.
  • URLPattern.pathname
    • : A string containing a pattern to match the pathname part of a URL.
  • URLPattern.port
    • : A string containing a pattern to match the port part of a URL.
  • URLPattern.protocol
    • : A string containing a pattern to match the protocol part of a URL.
  • URLPattern.search
    • : A string containing a pattern to match the search part of a URL.
  • URLPattern.username
    • : A string containing a pattern to match the username part of a URL.

Instance methods

  • URLPattern.exec
    • : Returns an object with the matched parts of the URL or null if the URL does not match.
  • URLPattern.test
    • : Returns true if the URL matches the given pattern, false otherwise.

Specifications

Browser compatibility

See also

  • A polyfill of URLPattern is available on GitHub
  • The pattern syntax used by URLPattern is similar to the syntax used by path-to-regexp

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

资料来源与适用范围

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

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

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