Skip to content

Commit 3c07b0a

Browse files
authored
Create 使用 URLPattern 匹配 URL 参数.js
1 parent 63a0a24 commit 3c07b0a

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const pattern = new URLPattern({ pathname: "/products/:id" });
2+
console.log(pattern.test("https://example.com/products/123")); // true
3+
4+
const pattern = new URLPattern({ pathname: "/books/:category/:id" });
5+
const result = pattern.exec("/books/classics/12345");
6+
7+
// `result.pathname.groups` will return:
8+
// { category: "classics", id: "12345" }
9+
const { category, id: bookId } = result.pathname.groups;

0 commit comments

Comments
 (0)