{"id":2858,"date":"2024-11-10T12:02:41","date_gmt":"2024-11-10T04:02:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2858\/"},"modified":"2024-11-10T12:02:41","modified_gmt":"2024-11-10T04:02:41","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%8c%b9%e9%85%8d%e4%b8%8d%e8%b6%85%e8%bf%875%e4%bd%8d%e7%9a%84%e6%95%b0%e5%ad%97%e6%88%96%e5%b8%a6%e5%b0%8f%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2858\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0d\u8d85\u8fc75\u4f4d\u7684\u6570\u5b57\u6216\u5e26\u5c0f\u6570\u70b9\u7684\u6570\u5b57\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173089622352980.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0d\u8d85\u8fc75\u4f4d\u7684\u6570\u5b57\u6216\u5e26\u5c0f\u6570\u70b9\u7684\u6570\u5b57\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0d\u8d85\u8fc75\u4f4d\u7684\u6570\u5b57\u6216\u5e26\u5c0f\u6570\u70b9\u7684\u6570\u5b57\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>js\u6b63\u5219\u5339\u914d\u6570\u5b57\u6216\u5e26\u5c0f\u6570\u70b9\u6570\u5b57\uff08\u6700\u591a5\u4f4d\uff09<\/strong><\/p>\n<p>\u95ee\u9898\u63cf\u8ff0\uff1a<\/p>\n<p>\u5982\u4f55\u5728javascript\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6570\u5b57\u6216\u5e26\u6709\u5c0f\u6570\u70b9\u7684\u6570\u5b57\uff0c\u4e14\u957f\u5ea6\u4e0d\u8d85\u8fc75\u4f4d\u6570\u5b57\uff1f<\/p>\n<p>\u7b54\u6848\uff1a<\/p>\n<p>\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u6ee1\u8db3\u8be5\u8981\u6c42\uff1a<\/p>\n<pre>^\\d(.|\\d){0,5}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790\uff1a<\/p>\n<ul>\n<li>^\uff1a\u4ee5\u5f00\u5934<\/li>\n<li>d\uff1a\u5339\u914d\u4e00\u4f4d\u6570\u5b57<\/li>\n<li>(.|d)\uff1a\u5339\u914d\u4e00\u4e2a\u5c0f\u6570\u70b9\u6216\u4e00\u4f4d\u6570\u5b57<\/li>\n<li>{0,5}\uff1a\u91cd\u590d\u524d\u4e00\u4e2a\u5143\u7d200\u52305\u6b21<\/li>\n<li>$\uff1a\u4ee5\u7ed3\u675f<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u6d4b\u8bd5\u5b57\u7b26\u4e32\nconst strings = ['1.0', '1.2.1', '2.2', '12345.6789'];\n\n\/\/ \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\nconst regex = \/^\\d(.|\\d){0,5}$\/;\n\n\/\/ \u904d\u5386\u5b57\u7b26\u4e32\u5e76\u5339\u914d\nstrings.foreach(string =&gt; {\n  const match = regex.test(string);\n  console.log(`${string}: ${match}`);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>1.0: true\n1.2.1: true\n2.2: true\n12345.6789: false<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0d\u8d85\u8fc75\u4f4d\u7684\u6570\u5b57\u6216\u5e26\u5c0f\u6570\u70b9\u7684\u6570\u5b57\uff1f\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>js\u6b63\u5219\u5339\u914d\u6570\u5b57\u6216\u5e26\u5c0f\u6570\u70b9\u6570\u5b57\uff08\u6700\u591a5\u4f4d\uff09 \u95ee\u9898\u63cf\u8ff0\uff1a \u5982\u4f55\u5728javascript\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6570\u5b57\u6216\u5e26\u6709\u5c0f\u6570\u70b9\u7684\u6570\u5b57\uff0c\u4e14\u957f\u5ea6\u4e0d\u8d85\u8fc75\u4f4d\u6570\u5b57\uff1f \u7b54\u6848\uff1a \u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u6ee1\u8db3\u8be5\u8981\u6c42\uff1a ^\\d(.|\\d){0,5} \u767b\u5f55\u540e\u590d\u5236 \u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790\uff1a ^\uff1a\u4ee5\u5f00\u5934 d\uff1a\u5339\u914d\u4e00\u4f4d\u6570\u5b57 (.|d)\uff1a\u5339\u914d\u4e00\u4e2a\u5c0f\u6570\u70b9\u6216\u4e00\u4f4d\u6570\u5b57 {0,5}\uff1a\u91cd\u590d\u524d\u4e00\u4e2a\u5143\u7d200\u52305\u6b21 $\uff1a\u4ee5\u7ed3\u675f \u793a\u4f8b\uff1a \/\/ \u6d4b\u8bd5\u5b57\u7b26\u4e32 const strings = [&#8216;1.0&#8217;, &#8216;1.2.1&#8217;, &#8216;2.2&#8217;, &#8216;12345.6789&#8217;]; \/\/ \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d const regex = \/^\\d(.|\\d){0,5}$\/; \/\/ \u904d\u5386\u5b57\u7b26\u4e32\u5e76\u5339\u914d strings.foreach(string =&gt; { const match = regex.test(string); console.log(`${string}: ${match}`); }); \u767b\u5f55\u540e\u590d\u5236 \u8f93\u51fa\uff1a 1.0: true 1.2.1: true 2.2: true 12345.6789: false \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0d\u8d85\u8fc75\u4f4d\u7684\u6570\u5b57\u6216\u5e26\u5c0f\u6570\u70b9\u7684\u6570\u5b57\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-2858","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2858","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=2858"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2858\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}