{"id":3271,"date":"2024-11-10T15:15:02","date_gmt":"2024-11-10T07:15:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3271\/"},"modified":"2024-11-10T15:15:02","modified_gmt":"2024-11-10T07:15:02","slug":"js%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%adurl","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3271\/","title":{"rendered":"js\u5982\u4f55\u5224\u65adurl"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u5224\u65ad url \u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 window.location \u5bf9\u8c61\u63d0\u53d6 url \u4fe1\u606f\uff0c\u4f8b\u5982\u4e3b\u673a\u540d\u548c\u67e5\u8be2\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 url \u5bf9\u8c61\u89e3\u6790\u548c\u64cd\u4f5c url\uff0c\u63d0\u4f9b\u9ad8\u7ea7\u65b9\u6cd5\u6765\u83b7\u53d6\u8def\u5f84\u548c\u67e5\u8be2\u53c2\u6570\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u548c\u63d0\u53d6 url \u6a21\u5f0f\uff0c\u9a8c\u8bc1 url \u7684\u6709\u6548\u6027\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110808515515106.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5224\u65adurl\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5224\u65adurl\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u5224\u65ad URL<\/strong><\/p>\n<p>\u5224\u65ad URL \u5728 JavaScript \u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u4efb\u52a1\uff0c\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u5355\u51fb\u7684\u94fe\u63a5\u3001\u89e3\u6790\u67e5\u8be2\u5b57\u7b26\u4e32\u6216\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u65b9\u6cd5\u6765\u5224\u65ad URL\uff1a<\/p>\n<p><strong>\u4f7f\u7528 window.location \u5bf9\u8c61<\/strong><\/p>\n<p>window.location \u5bf9\u8c61\u5305\u542b\u6709\u5173\u5f53\u524d URL \u7684\u4fe1\u606f\uff1a<\/p>\n<pre>console.log(window.location.href); \/\/ \u8f93\u51fa\u5b8c\u6574\u7684 URL\nconsole.log(window.location.hostname); \/\/ \u8f93\u51fa\u4e3b\u673a\u540d\nconsole.log(window.location.pathname); \/\/ \u8f93\u51fa\u8def\u5f84\nconsole.log(window.location.search); \/\/ \u8f93\u51fa\u67e5\u8be2\u5b57\u7b26\u4e32<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 URL \u5bf9\u8c61<\/strong><\/p>\n<p>URL \u5bf9\u8c61\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\u6765\u89e3\u6790\u548c\u5904\u7406 URL\uff1a<\/p>\n<pre>const url = new URL('https:\/\/example.com\/path\/file.html?query=value');\nconsole.log(url.href); \/\/ \u8f93\u51fa\u5b8c\u6574\u7684 URL\nconsole.log(url.hostname); \/\/ \u8f93\u51fa\u4e3b\u673a\u540d\nconsole.log(url.pathname); \/\/ \u8f93\u51fa\u8def\u5f84\nconsole.log(url.search); \/\/ \u8f93\u51fa\u67e5\u8be2\u5b57\u7b26\u4e32\nconsole.log(url.searchParams.get('query')); \/\/ \u8f93\u51fa\u67e5\u8be2\u53c2\u6570\u503c<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u5339\u914d\u548c\u63d0\u53d6 URL \u4e2d\u7684\u6a21\u5f0f\uff1a<\/p>\n<pre>const url = 'https:\/\/example.com\/path\/file.html?query=value';\nconst regex = \/^(https?:\/\/)?([da-z.-]+).([a-z.]{2,6})([\/w .-]*)*\/?$\/;\nconst match = url.match(regex);\nif (match) {\n  console.log('Valid URL');\n} else {\n  console.log('Invalid URL');\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u4f7f\u7528 JavaScript \u5224\u65ad URL \u53ef\u4ee5\u901a\u8fc7 window.location \u5bf9\u8c61\u3001URL \u5bf9\u8c61\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u8981\u6267\u884c\u7684\u4efb\u52a1\u548c\u6240\u9700\u7684\u7279\u5b9a\u4fe1\u606f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5224\u65adurl\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\u5224\u65ad url \u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 window.location \u5bf9\u8c61\u63d0\u53d6 url \u4fe1\u606f\uff0c\u4f8b\u5982\u4e3b\u673a\u540d\u548c\u67e5\u8be2\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 url \u5bf9\u8c61\u89e3\u6790\u548c\u64cd\u4f5c url\uff0c\u63d0\u4f9b\u9ad8\u7ea7\u65b9\u6cd5\u6765\u83b7\u53d6\u8def\u5f84\u548c\u67e5\u8be2\u53c2\u6570\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u548c\u63d0\u53d6 url \u6a21\u5f0f\uff0c\u9a8c\u8bc1 url \u7684\u6709\u6548\u6027\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u5224\u65ad URL \u5224\u65ad URL \u5728 JavaScript \u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u4efb\u52a1\uff0c\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u5355\u51fb\u7684\u94fe\u63a5\u3001\u89e3\u6790\u67e5\u8be2\u5b57\u7b26\u4e32\u6216\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u65b9\u6cd5\u6765\u5224\u65ad URL\uff1a \u4f7f\u7528 window.location \u5bf9\u8c61 window.location \u5bf9\u8c61\u5305\u542b\u6709\u5173\u5f53\u524d URL \u7684\u4fe1\u606f\uff1a console.log(window.location.href); \/\/ \u8f93\u51fa\u5b8c\u6574\u7684 URL console.log(window.location.hostname); \/\/ \u8f93\u51fa\u4e3b\u673a\u540d console.log(window.location.pathname); \/\/ \u8f93\u51fa\u8def\u5f84 console.log(window.location.search); \/\/ \u8f93\u51fa\u67e5\u8be2\u5b57\u7b26\u4e32 \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 URL \u5bf9\u8c61 URL \u5bf9\u8c61\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\u6765\u89e3\u6790\u548c\u5904\u7406 URL\uff1a const url [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3271","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3271"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3271\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}