URL Pattern API

URL Pattern API

创建 URL 模式匹配器。这些模式能够与完整的 URLURL 的各个组成部分进行匹配

  • 模式语法基于 path-to-regexp 库。模式可包含:

    • 将被精确匹配的文字字符串。
    • 匹配任何字符的通配符 (/posts/*)
    • 命名组 (/books/:id),提取匹配URL的一部分。
    • 非捕获组 (/books{/old}?),使模式的一部分成为可选的 或者被多次匹配。
    • RegExp(/books/(\\d+)),使任意复杂 正则表达式匹配有一些限制。
const pattern = new URLPattern({ pathname: "/list/:id" });
console.log(pattern.test("http://localhost:3000/list/123")); // true
console.log(pattern.exec("http://localhost:3000/list/123").pathname.groups); // { id: "123" }
posted @ 2024-04-30 13:43  _clai  阅读(3)  评论(0编辑  收藏  举报