يطابق URLPattern عناوين URL كاملة أو مكوّنات URL منفردة.
const users = new URLPattern({ pathname: "/users/:id" });
users.test({ pathname: "/users/42" }); // true
users.exec({ pathname: "/users/42" }).pathname.groups.id; // "42"يقبل الإنشاء سلسلة نمط مع عنوان URL أساسي اختياري، أو قاموسًا
يتضمن protocol وusername وpassword وhostname وport
وpathname وsearch وhash وbaseURL. ويطبّق
الخيار ignoreCase مطابقة لا تميّز حالة الأحرف.
صيغة الأنماط
| الصيغة | المعنى |
|---|---|
:name | التقاط مقطع مسمّى. |
:name(regex) | تعبير نمطي مخصّص مسمّى. |
(regex) | تعبير نمطي مخصّص غير مسمّى. |
* | حرف بدل كامل. |
{...} | مجموعة. |
?, +, * | معدِّل اختياري أو تكراري. |
\x | حرف حرفي مُهرَّب. |
const api = new URLPattern("https://*.example.com/api/:resource");
api.exec("https://v2.example.com/api/widgets")
.pathname.groups.resource; // "widgets"
const numeric = new URLPattern({ pathname: "/items/:id(\\d+)" });
numeric.test({ pathname: "/items/123" }); // true
const relative = new URLPattern("/items/:id", "https://example.com/");
relative.test("https://example.com/items/7"); // trueيعيد test() قيمة منطقية. ويعيد exec() القيمة null أو نتيجة تتضمن مدخل كل
مكوّن ومجموعات الالتقاط فيه. وتكشف سمات المكوّنات للقراءة فقط
النمط المُجمَّع، ويحدد hasRegExpGroups مجموعات التعابير النمطية الصريحة.
Reference
Status: Full · Not measured · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
URLPattern | class | Full | protocol, username, password, hostname, port, pathname, search, hash, hasRegExpGroups, test(), exec() |