{"id":310,"date":"2024-12-17T08:55:19","date_gmt":"2024-12-17T00:55:19","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=310"},"modified":"2024-12-18T23:06:37","modified_gmt":"2024-12-18T15:06:37","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%9c%a8-javascript-%e4%b8%ad%e6%8f%90%e5%8f%96%e4%b8%a4%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b9%8b%e9%97%b4","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/17\/%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%9c%a8-javascript-%e4%b8%ad%e6%8f%90%e5%8f%96%e4%b8%a4%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b9%8b%e9%97%b4\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5728 JavaScript \u4e2d\u63d0\u53d6\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u5b50\u5b57\u7b26\u4e32\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 JavaScript \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528 <strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong> \u7684\u6355\u83b7\u7ec4\uff08capturing groups\uff09\u6765\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5339\u914d\u4e24\u4e2a\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\uff08\u6216\u6a21\u5f0f\uff09\u4e4b\u95f4\u7684\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528\u6355\u83b7\u7ec4\u6765\u63d0\u53d6\u5b83\u4eec\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bed\u6cd5\u793a\u4f8b\uff1a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u8981\u63d0\u53d6 <code>start<\/code> \u548c <code>end<\/code> \u4e4b\u95f4\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const regex = \/start(.*?)end\/;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>start<\/code> \u548c <code>end<\/code> \u662f\u4f60\u60f3\u8981\u63d0\u53d6\u5185\u5bb9\u7684\u8fb9\u754c\u3002<\/li>\n\n\n\n<li><code>(.*?)<\/code> \u662f\u6355\u83b7\u7ec4\uff0c\u5b83\u4f1a\u5339\u914d <code>start<\/code> \u548c <code>end<\/code> \u4e4b\u95f4\u7684\u6240\u6709\u5185\u5bb9\uff1a<\/li>\n\n\n\n<li><code>.<\/code> \u8868\u793a\u5339\u914d\u4efb\u4f55\u5b57\u7b26\uff08\u9664\u4e86\u6362\u884c\u7b26\uff09\u3002<\/li>\n\n\n\n<li><code>*<\/code> \u8868\u793a\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\u3002<\/li>\n\n\n\n<li><code>?<\/code> \u662f\u975e\u8d2a\u5a6a\u5339\u914d\u7b26\uff0c\u610f\u5473\u7740\u5b83\u4f1a\u5c3d\u53ef\u80fd\u5339\u914d\u6700\u5c11\u7684\u5b57\u7b26\uff0c\u4ee5\u907f\u514d\u5339\u914d\u5230\u4e0b\u4e00\u4e2a <code>end<\/code> \u4e4b\u524d\u7684\u6240\u6709\u5185\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u60f3\u4ece\u4e2d\u63d0\u53d6\u4e24\u4e2a\u7279\u5b9a\u5b57\u7b26\u4e32\uff08\u4f8b\u5982 <code>start<\/code> \u548c <code>end<\/code>\uff09\u4e4b\u95f4\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const str = \"Here is the startHello Worldend and more text.\";\nconst regex = \/start(.*?)end\/;\nconst match = str.match(regex);\n\nif (match) {\n  console.log(\"\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32:\", match&#91;1]);  \/\/ match&#91;1] \u662f\u7b2c\u4e00\u4e2a\u6355\u83b7\u7ec4\n} else {\n  console.log(\"\u672a\u627e\u5230\u5339\u914d\u7684\u5185\u5bb9\");\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed3\u679c\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32: Hello World<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>str.match(regex)<\/code> \u4f1a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u5b8c\u6574\u7684\u5339\u914d\u5b57\u7b26\u4e32\uff08\u5373\u5305\u542b <code>start<\/code> \u548c <code>end<\/code> \u7684\u90e8\u5206\uff09\uff0c\u800c\u6570\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u7b2c\u4e00\u4e2a\u6355\u83b7\u7ec4\uff0c\u5373 <code>start<\/code> \u548c <code>end<\/code> \u4e4b\u95f4\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><code>match[1]<\/code> \u5c31\u662f\u4f60\u63d0\u53d6\u7684 <code>start<\/code> \u548c <code>end<\/code> \u4e4b\u95f4\u7684\u5185\u5bb9\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5904\u7406\u591a\u4e2a\u5339\u914d\uff1a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u7684\u5b57\u7b26\u4e32\u4e2d\u6709\u591a\u4e2a <code>start<\/code> \u548c <code>end<\/code> \u4e4b\u95f4\u7684\u5339\u914d\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>g<\/code> \u6807\u5fd7\u6765\u5168\u5c40\u5339\u914d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const str = \"startFirstend some text startSecondend more text startThirdend.\";\nconst regex = \/start(.*?)end\/g;\nlet match;\nwhile ((match = regex.exec(str)) !== null) {\n  console.log(\"\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32:\", match&#91;1]);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed3\u679c\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32: First\n\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32: Second\n\u63d0\u53d6\u7684\u5b50\u5b57\u7b26\u4e32: Third<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5176\u4ed6\u60c5\u51b5\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c <code>start<\/code> \u548c <code>end<\/code> \u662f\u53ef\u53d8\u7684\u5b57\u7b26\u4e32\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\u6765\u52a8\u6001\u6784\u9020\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u9700\u8981\u63d0\u53d6\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5229\u7528 <code>exec()<\/code> \u6216 <code>matchAll()<\/code> \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u65b9\u6cd5\u4f1a\u5e2e\u52a9\u4f60\u5728 JavaScript \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u4e24\u4e2a\u7279\u5b9a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528 \u6b63\u5219\u8868\u8fbe\u5f0f \u7684\u6355\u83b7\u7ec4\uff08c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/310\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}