{"id":52145,"date":"2024-12-03T12:16:43","date_gmt":"2024-12-03T04:16:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52145\/"},"modified":"2024-12-03T12:16:43","modified_gmt":"2024-12-03T04:16:43","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8javascript%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e4%bb%8eurl%e4%b8%ad%e6%8f%90%e5%8f%96%e6%96%9c%e6%9d%a0%e4%b9%8b%e9%97%b4%e7%9a%84%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52145\/","title":{"rendered":"\u5982\u4f55\u5229\u7528JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceURL\u4e2d\u63d0\u53d6\u659c\u6760\u4e4b\u95f4\u7684\u503c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5229\u7528JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceURL\u4e2d\u63d0\u53d6\u659c\u6760\u4e4b\u95f4\u7684\u503c\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5229\u7528JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceURL\u4e2d\u63d0\u53d6\u659c\u6760\u4e4b\u95f4\u7684\u503c\uff1f\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731991726673c18ae53b66.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceURL\u4e2d\u63d0\u53d6\u659c\u6760\u4e4b\u95f4\u7684\u503c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceURL\u4e2d\u63d0\u53d6\u659c\u6760\u4e4b\u95f4\u7684\u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>js\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6url\u4e2d\u659c\u6760\u4e4b\u95f4\u7684\u503c<\/strong><\/p>\n<p>\u5982\u4f55\u5229\u7528javascript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceurl\u4e2d\u63d0\u53d6\u659c\u6760\uff08\/\uff09\u4e4b\u95f4\u7684\u503c\uff1f<\/p>\n<p>\u4e0b\u9762\u63d0\u4f9b\u4e86\u4e00\u6bb5\u4ee3\u7801\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff1a<\/p>\n<pre>function extractinfo(pattern, url) {\n  const urlregex = new regexp(pattern.replace(\/(\\{|:)[\\w-]+(\\}|:)\/g, \"[\\\\w-]+\").replace(\/\\\/\/g, \"\\\/\"));\n  if(!urlregex.test(url)) {\n      throw new error(\"\u8f93\u5165\u7684url\u4e0d\u7b26\u5408pattern\");\n  }\n  let keys = pattern.split(\"\/\");\n  let values = url.replace(\/(\\?|#).*\/, \"\").split(\"\/\");\n  let result = {};\n  keys.foreach((v,i) =&gt; {\n      if(\/(\\{|:)\\w+(\\}|:)\/.test(v)) {\n          result[v.replace(\/(\\{|:|\\})\/g,\"\")] = values[i];\n      }\n  })\n  return result;\n}<\/pre>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>pattern<\/strong>\uff1a\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<li><strong>url<\/strong>\uff1a\u8981\u63d0\u53d6\u4fe1\u606f\u7684url\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre>let pattern = \"{note}\/:pakid:\/corrinsdetail\/{corrinsaleid}\/{corrinspakid}\/{corrinsinsta}\/{corrinsvernr}\/{verfrom}\/{verto}\/{name}\/:tab:\";\nlet url = \"0001668882\/41\/corrinsdetail\/0001023444\/41\/0020751259\/0005\/756\/756\/sap_basis\/validitydetails\";\nconsole.log(extractinfo(pattern, url));<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre>{\n  note: \"0001668882\",\n  pakid: \"41\",\n  corrInsAleid: \"0001023444\",\n  corrInsPakId: \"41\",\n  corrInsInsta: \"0020751259\",\n  corrInsVernr: \"0005\",\n  verFrom: \"756\",\n  verTo: \"756\",\n  name: \"SAP_BASIS\",\n  tab: \"ValidityDetails\"\n}<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5229\u7528JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceURL\u4e2d\u63d0\u53d6\u659c\u6760\u4e4b\u95f4\u7684\u503c\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Win10\u7cfb\u7edf\u600e\u4e48\u5907\u4efd\u548c\u8fd8\u539f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceURL\u4e2d\u63d0\u53d6\u659c\u6760\u4e4b\u95f4\u7684\u503c\uff1f \u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300a\u5982\u4f55\u5229\u7528JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceURL\u4e2d\u63d0\u53d6\u659c\u6760\u4e4b\u95f4\u7684\u503c\uff1f\u300b\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9\u6587\u7ae0\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01 js\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6url\u4e2d\u659c\u6760\u4e4b\u95f4\u7684\u503c \u5982\u4f55\u5229\u7528javascript\u6b63\u5219\u8868\u8fbe\u5f0f\u4eceurl\u4e2d\u63d0\u53d6\u659c\u6760\uff08\/\uff09\u4e4b\u95f4\u7684\u503c\uff1f \u4e0b\u9762\u63d0\u4f9b\u4e86\u4e00\u6bb5\u4ee3\u7801\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff1a function extractinfo(pattern, url) { const urlregex = new regexp(pattern.replace(\/(\\{|:)[\\w-]+(\\}|:)\/g, &#8220;[\\\\w-]+&#8221;).replace(\/\\\/\/g, &#8220;\\\/&#8221;)); if(!urlregex.test(url)) { throw new error(&#8220;\u8f93\u5165\u7684url\u4e0d\u7b26\u5408pattern&#8221;); } let keys = pattern.split(&#8220;\/&#8221;); let values = url.replace(\/(\\?|#).*\/, &#8220;&#8221;).split(&#8220;\/&#8221;); let result = {}; keys.foreach((v,i) =&gt; { if(\/(\\{|:)\\w+(\\}|:)\/.test(v)) { result[v.replace(\/(\\{|:|\\})\/g,&#8221;&#8221;)] = values[i]; } }) return result; } \u4f7f\u7528\u65b9\u6cd5\uff1a pattern\uff1a\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 url\uff1a\u8981\u63d0\u53d6\u4fe1\u606f\u7684url\u3002 \u4f7f\u7528\u793a\u4f8b\uff1a let pattern = [&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-52145","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52145","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=52145"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52145\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}