{"id":32377,"date":"2024-11-25T17:46:51","date_gmt":"2024-11-25T09:46:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32377\/"},"modified":"2024-11-25T17:46:51","modified_gmt":"2024-11-25T09:46:51","slug":"javascript%e4%b8%ad%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e7%9a%84%e5%88%9d%e5%ad%a6%e8%80%85%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32377\/","title":{"rendered":"JavaScript\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u521d\u5b66\u8005\u6307\u5357"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169337910635175.jpg\" class=\"aligncenter\" title=\"JavaScript\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u521d\u5b66\u8005\u6307\u5357\u63d2\u56fe\" alt=\"JavaScript\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u521d\u5b66\u8005\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p>\u6bcf\u4e2a\u4f7f\u7528 JavaScript \u7684\u4eba\u90fd\u5fc5\u987b\u5728\u67d0\u4e00\u65f6\u523b\u5904\u7406\u5b57\u7b26\u4e32\u3002\u6709\u65f6\uff0c\u60a8\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u53e6\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u51fa\u53bb\u3002\u5176\u4ed6\u65f6\u5019\uff0c\u60a8\u5fc5\u987b\u68c0\u67e5\u5b83\u5e76\u67e5\u770b\u5b83\u662f\u5426\u5305\u542b\u7279\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u7136\u800c\uff0c\u4e8b\u60c5\u5e76\u4e0d\u603b\u662f\u90a3\u4e48\u5bb9\u6613\u3002\u6709\u65f6\u60a8\u4e0d\u4f1a\u67e5\u627e\u7279\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u67e5\u627e\u9075\u5faa\u7279\u5b9a\u6a21\u5f0f\u7684\u4e00\u7ec4\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u5047\u8bbe\u60a8\u5fc5\u987b\u5c06\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u201cApples\u201d\u66ff\u6362\u4e3a\u201capples\u201d\u3002\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528 theMainString.replace(&#8220;Apples&#8221;, &#8220;apples&#8221;)\u3002\u597d\uff0c\u6613\u4e8e\u3002 <\/p>\n<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u73b0\u5728\u5047\u8bbe\u60a8\u8fd8\u5fc5\u987b\u5c06\u201cappLes\u201d\u66ff\u6362\u4e3a\u201capples\u201d\u3002\u540c\u6837\uff0c\u201cappLES\u201d\u4e5f\u5e94\u8be5\u53d8\u6210\u201capples\u201d\u3002\u57fa\u672c\u4e0a\uff0c\u201cApple\u201d\u7684\u6240\u6709\u5927\u5c0f\u5199\u53d8\u4f53\u90fd\u9700\u8981\u66f4\u6539\u4e3a\u201capple\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f20\u9012\u7b80\u5355\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u5c06\u4e0d\u518d\u5b9e\u7528\u6216\u9ad8\u6548\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6b66\u4e4b\u5730 &#8211; \u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6807\u5fd7 i \u5e76\u5b8c\u6210\u5b83\u3002\u4f7f\u7528\u8be5\u6807\u5fd7\u540e\uff0c\u539f\u59cb\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u201cApples\u201d\u3001\u201cAPPles\u201d\u3001\u201cApPlEs\u201d\u6216\u201cApples\u201d\u5e76\u4e0d\u91cd\u8981\u3002\u8be5\u5355\u8bcd\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u5c06\u66ff\u6362\u4e3a\u201capples\u201d\u3002<\/p>\n<p>\u5c31\u50cf\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6807\u5fd7\u4e00\u6837\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u529f\u80fd\uff0c\u672c\u6559\u7a0b\u5c06\u4ecb\u7ecd\u8fd9\u4e9b\u529f\u80fd\u3002 <\/p>\n<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<h2>\u5728 JavaScript \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<p>\u60a8\u5fc5\u987b\u4f7f\u7528\u7a0d\u5fae\u4e0d\u540c\u7684\u8bed\u6cd5\u6765\u6307\u793a\u4e0d\u540c String \u65b9\u6cd5\u5185\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4e0e\u7528\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u7b80\u5355\u5b57\u7b26\u4e32\u4e0d\u540c\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7531\u62ec\u5728\u659c\u6760\u4e4b\u95f4\u7684\u6a21\u5f0f\u7ec4\u6210\u3002\u60a8\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u7684\u4efb\u4f55\u6807\u5fd7\u90fd\u5c06\u9644\u52a0\u5728\u7b2c\u4e8c\u4e2a\u659c\u6760\u4e4b\u540e\u3002<\/p>\n<p>\u56de\u5230\u524d\u9762\u7684\u793a\u4f8b\uff0c\u4e0b\u9762\u662f replace() \u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u7b80\u5355\u5b57\u7b26\u4e32\u7684\u6837\u5b50\u3002<\/p>\n<pre>\"I ate Apples\".replace(\"Apples\", \"apples\");\n\/\/ I ate apples\n\n\"I ate Apples\".replace(\/Apples\/i, \"apples\");\n\/\/ I ate apples\n\n\"I ate aPPles\".replace(\"Apples\", \"apples\");\n\/\/ I ate aPPles\n\n\"I ate aPPles\".replace(\/Apples\/i, \"apples\");\n\/\/ I ate apples\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\u90fd\u6709\u6548\u3002\u6211\u4eec\u73b0\u5728\u5c06\u4e86\u89e3\u6709\u5173\u6784\u6210\u6b63\u5219\u8868\u8fbe\u5f0f\u5185\u6a21\u5f0f\u7684\u6807\u5fd7\u548c\u7279\u6b8a\u5b57\u7b26\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<h2>\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u53cd\u659c\u6760<\/h2>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u666e\u901a\u5b57\u7b26\u524d\u6dfb\u52a0\u53cd\u659c\u6760\u5c06\u5176\u8f6c\u6362\u4e3a\u7279\u6b8a\u5b57\u7b26\u3002\u540c\u6837\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u7279\u6b8a\u5b57\u7b26\u524d\u6dfb\u52a0\u53cd\u659c\u6760\u5c06\u5176\u8f6c\u6362\u4e3a\u666e\u901a\u5b57\u7b26\u3002<\/p>\n<p>\u4f8b\u5982\uff0cd \u4e0d\u662f\u7279\u6b8a\u5b57\u7b26\u3002\u7136\u800c\uff0c d \u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u5b57\u7b26\u3002\u540c\u6837\uff0cD \u4e5f\u4e0d\u662f\u7279\u6b8a\u5b57\u7b26\uff0c\u4f46 D \u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u975e\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<p>\u6570\u5b57\u5b57\u7b26\u5305\u62ec 0\u30011\u30012\u30013\u30014\u30015\u30016\u30017\u30018 \u548c 9\u3002\u5f53\u60a8\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528 d \u65f6\uff0c\u5b83\u5c06\u5339\u914d\u4ee5\u4e0b\u4efb\u4f55\u4e00\u4e2a\u4e5d\u4e2a\u5b57\u7b26\u3002\u5f53\u60a8\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528 D \u65f6\uff0c\u5b83\u5c06\u5339\u914d\u6240\u6709\u975e\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u5e94\u8be5\u53ef\u4ee5\u8ba9\u4e8b\u60c5\u53d8\u5f97\u6e05\u695a\u3002<\/p>\n<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\"L8\".replace(\/d\/i, \"E\");\n\/\/ LE\n\n\"L8\".replace(\/D\/i, \"E\");\n\/\/ E8\n\n\"LLLLL8\".replace(\/D\/i, \"E\");\n\/\/ ELLLL8\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p> \u60a8\u5e94\u8be5\u6ce8\u610f\uff0c\u5728\u7b2c\u4e09\u79cd\u60c5\u51b5\u4e0b\uff0c\u4ec5\u66ff\u6362\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5b57\u7b26\u3002\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6807\u5fd7\u6765\u66ff\u6362\u6240\u6709\u5339\u914d\u9879\u3002\u6211\u4eec\u7a0d\u540e\u5c06\u4e86\u89e3\u6b64\u7c7b\u6807\u5fd7\u3002<\/p>\n<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5c31\u50cf d \u548c D \u4e00\u6837\uff0c\u8fd8\u6709\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u5e8f\u5217\u3002<\/p>\n<ol>\n<li>\u60a8\u53ef\u4ee5\u4f7f\u7528 w \u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u4efb\u4f55\u201cword\u201d\u5b57\u7b26\u3002\u8fd9\u91cc\uff0c\u5355\u8bcd\u5b57\u7b26\u662f\u6307A-Z\u3001a-z\u30010-9\u548c_\u3002\u56e0\u6b64\uff0c\u57fa\u672c\u4e0a\uff0c\u5b83\u5c06\u5339\u914d\u6240\u6709\u6570\u5b57\u3001\u6240\u6709\u5c0f\u5199\u548c\u5927\u5199\u5b57\u6bcd\u4ee5\u53ca\u4e0b\u5212\u7ebf\u3002<\/li>\n<li>\u60a8\u53ef\u4ee5\u4f7f\u7528 W \u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u4efb\u4f55\u975e\u5355\u8bcd\u5b57\u7b26\u3002\u5b83\u5c06\u5339\u914d %\u3001$\u3001#\u3001 $ \u7b49\u5b57\u7b26\u3002<\/li>\n<li>\u60a8\u53ef\u4ee5\u4f7f\u7528 s \u6765\u5339\u914d\u5355\u4e2a\u7a7a\u767d\u5b57\u7b26\uff0c\u5176\u4e2d\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u9875\u7b26\u548c\u6362\u884c\u7b26\u3002\u540c\u6837\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 S \u6765\u5339\u914d\u9664\u7a7a\u683c\u4e4b\u5916\u7684\u6240\u6709\u5176\u4ed6\u5b57\u7b26\u3002<\/li>\n<li>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 \u3001 \u3001 \u3001 \u548c \uff0c\u5206\u522b\u4ee3\u8868\u6362\u9875\u3001\u6362\u884c\u3001\u56de\u8f66\u3001\u6c34\u5e73\u5236\u8868\u7b26\u548c\u5782\u76f4\u5236\u8868\u7b26\u3002<\/li>\n<\/ol>\n<p>\u6709\u65f6\uff0c\u60a8\u4f1a\u9047\u5230\u9700\u8981\u7528\u66ff\u4ee3\u8bcd\u66ff\u6362\u67d0\u4e2a\u8bcd\u7684\u60c5\u51b5\uff0c\u4f46\u524d\u63d0\u662f\u5b83<em>\u4e0d\u662f<\/em><strong> <\/strong>\u8f83\u5927\u8bcd\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4ee5\u4e0b\u53e5\u5b50\uff1a<\/p>\n<p>\u201c\u5e94\u7528\u7a0b\u5e8f\u4e0a\u53d1\u5e03\u4e86\u5927\u91cf\u83e0\u841d\u56fe\u7247\u201d\u3002<\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u201capp\u201d\u4e00\u8bcd\u66ff\u6362\u4e3a\u201cboard\u201d\u3002\u7136\u800c\uff0c\u4f7f\u7528\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u4f1a\u5c06\u201capple\u201d\u53d8\u6210\u201cboardle\u201d\uff0c\u6700\u7ec8\u7684\u53e5\u5b50\u5c06\u53d8\u6210\uff1a<\/p>\n<p>\u201c\u5e94\u7528\u7a0b\u5e8f\u4e0a\u53d1\u5e03\u4e86\u5f88\u591a\u677e\u677f\u56fe\u50cf\u201d\u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u53e6\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\u5e8f\u5217\uff1a\u3002\u8fd9\u4f1a\u68c0\u67e5\u5355\u8bcd\u8fb9\u754c\u3002\u5355\u8bcd\u8fb9\u754c\u662f\u901a\u8fc7\u4f7f\u7528\u4efb\u4f55\u975e\u5355\u8bcd\u5b57\u7b26\uff08\u5982\u7a7a\u683c\u3001\u201c$\u201d\u3001\u201c%\u201d\u3001\u201c#\u201d\u7b49\uff09\u5f62\u6210\u7684\u3002\u4e0d\u8fc7\u8bf7\u6ce8\u610f\uff0c\u5b83\u8fd8\u5305\u62ec\u91cd\u97f3\u5b57\u7b26\uff0c\u5982\u201c\u00fc\u201d\u3002<\/p>\n<pre>\"A lot of pineapple images were posted on the app\".replace(\/app\/, \"board\");\n\/\/ A lot of pineboardle images were posted on the app\n\n\"A lot of pineapple images were posted on the app\".replace(\/app\/, \"board\");\n\/\/ A lot of pineapple images were posted on the board\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u540c\u6837\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<strong> <\/strong>B \u6765\u5339\u914d\u975e\u5355\u8bcd\u8fb9\u754c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 B \u4ec5\u5339\u914d\u4f4d\u4e8e\u53e6\u4e00\u4e2a\u5355\u8bcd\uff08\u5982\u201cpineapple\u201d\uff09\u5185\u7684\u201capp\u201d\u3002<\/p>\n<h2>\u5339\u914d\u6a21\u5f0f\u201cn\u201d\u6b21<\/h2>\n<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 ^ \u544a\u8bc9 JavaScript \u4ec5\u67e5\u770b\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u4ee5\u8fdb\u884c\u5339\u914d\u3002\u540c\u6837\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 $ \u4ec5\u67e5\u770b\u5b57\u7b26\u4e32\u672b\u5c3e\u7684\u5339\u914d\u9879\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 *<strong> <\/strong> \u6765\u5339\u914d\u524d\u9762\u7684\u8868\u8fbe\u5f0f 0 \u6b21\u6216\u591a\u6b21\u3002\u4f8b\u5982\uff0c\/Ap*\/ \u5c06\u5339\u914d <strong>A<\/strong>\u3001<strong>Ap<\/strong>\u3001<strong>App<\/strong>\u3001<strong>Appp\u5f3a&gt;\uff0c\u7b49\u7b49\u3002<\/strong><\/p>\n<p>\u7c7b\u4f3c\u5730\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 + \u6765\u5339\u914d\u524d\u9762\u7684\u8868\u8fbe\u5f0f 1 \u6b21\u6216\u591a\u6b21\u3002\u4f8b\u5982\uff0c\/Ap+\/ \u5c06\u5339\u914d <strong>Ap<\/strong>\u3001<strong>App<\/strong>\u3001<strong>Appp<\/strong> \u7b49\u3002\u8fd9\u6b21\u8868\u8fbe\u5f0f\u5c06\u4e0d\u4f1a\u5339\u914d\u5355\u4e2a <strong>A<\/strong>\u3002<\/p>\n<p>\u6709\u65f6\uff0c\u60a8\u53ea\u60f3\u5339\u914d\u7ed9\u5b9a\u6a21\u5f0f\u7684\u7279\u5b9a\u51fa\u73b0\u6b21\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528 {n} \u5b57\u7b26\u5e8f\u5217\uff0c\u5176\u4e2d n \u662f\u6570\u5b57\u3002\u4f8b\u5982\uff0c\/Ap{2}\/ \u5c06\u5339\u914d <strong>App<\/strong>\uff0c\u4f46\u4e0d\u5339\u914d <strong>Ap<\/strong>\u3002\u5b83\u8fd8\u5c06\u5339\u914d <strong>Appp<\/strong> \u4e2d\u7684\u524d\u4e24\u4e2a\u201cp\u201d\uff0c\u5e76\u4fdd\u6301\u7b2c\u4e09\u4e2a\u201cp\u201d\u4e0d\u53d8\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 {n,} \u6765\u5339\u914d\u7ed9\u5b9a\u8868\u8fbe\u5f0f\u7684\u81f3\u5c11\u201cn\u201d\u6b21\u51fa\u73b0\u3002\u8fd9\u610f\u5473\u7740 \/Ap{2,}\/ \u5c06\u5339\u914d <strong>App<\/strong>\uff0c\u4f46\u4e0d\u5339\u914d <strong>Ap<\/strong>\u3002\u5b83\u8fd8\u5c06\u5339\u914d <strong>Apppp<\/strong> \u4e2d\u7684\u6240\u6709\u201cp\u201d\uff0c\u5e76\u5c06\u5b83\u4eec\u66ff\u6362\u4e3a\u60a8\u7684\u66ff\u6362\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 {n,m} \u6307\u5b9a\u6700\u5c0f\u548c\u6700\u5927\u6570\u91cf\uff0c\u5e76\u9650\u5236\u7ed9\u5b9a\u8868\u8fbe\u5f0f\u5e94\u5339\u914d\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff0c\/Ap{2,4}\/ \u5c06\u5339\u914d <strong>App<\/strong>\u3001<strong>Appp<\/strong> \u548c <strong>Apppp<\/strong>\u3002\u5b83\u8fd8\u5c06\u5339\u914d <strong>Apppppp<\/strong> \u4e2d\u7684\u524d\u56db\u4e2a\u201cp\u201d\uff0c\u5e76\u4fdd\u6301\u5176\u4f59\u90e8\u5206\u4e0d\u53d8\u3002<\/p>\n<pre>\"Apppppples\".replace(\/Ap*\/, \"App\");\n\/\/ Apples\n\n\"Ales\".replace(\/Ap*\/, \"App\");\n\/\/ Apples\n\n\"Appppples\".replace(\/Ap{2}\/, \"Add\");\n\/\/ Addppples\n\n\"Appppples\".replace(\/Ap{2,}\/, \"Add\");\n\/\/ Addles\n\n\"Appppples\".replace(\/Ap{2,4}\/, \"Add\");\n\/\/ Addples\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u4f7f\u7528\u62ec\u53f7\u6765\u8bb0\u4f4f\u5339\u914d<\/h2>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u53ea\u7528\u5e38\u91cf\u5b57\u7b26\u4e32\u66ff\u6362\u4e86\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u66ff\u6362\u59cb\u7ec8\u662f\u201cAdd\u201d\u3002\u6709\u65f6\uff0c\u60a8\u5fc5\u987b\u5728\u7ed9\u5b9a\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6a21\u5f0f\u5339\u914d\uff0c\u7136\u540e\u5c06\u5176\u66ff\u6362\u4e3a\u6a21\u5f0f\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<p> \u5047\u8bbe\u60a8\u5fc5\u987b\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u6216\u66f4\u591a\u5b57\u6bcd\u7684\u5355\u8bcd\uff0c\u7136\u540e\u5728\u8be5\u5355\u8bcd\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u201cs\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5c06\u65e0\u6cd5\u4f7f\u7528\u5e38\u91cf\u5b57\u7b26\u4e32\u503c\u4f5c\u4e3a\u66ff\u6362\uff0c\u56e0\u4e3a\u6700\u7ec8\u503c\u53d6\u51b3\u4e8e\u5339\u914d\u6a21\u5f0f\u672c\u8eab\u3002<\/p>\n<pre>\"I like Apple\".replace(\/(w{5,})\/, '$1s');\n\/\/ I like Apples\n\n\"I like Banana\".replace(\/(w{5,})\/, '$1s');\n\/\/ I like Bananas\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f46\u60a8\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u6280\u672f\u5728\u5185\u5b58\u4e2d\u4fdd\u7559\u591a\u4e2a\u5339\u914d\u6a21\u5f0f\u3002\u5b8c\u6574\u5339\u914d\u4e2d\u7684\u5b50\u6a21\u5f0f\u6570\u91cf\u5c06\u7531\u4f7f\u7528\u7684\u62ec\u53f7\u6570\u91cf\u51b3\u5b9a\u3002<\/p>\n<p>\u5728\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u5b50\u5339\u914d\u5c06\u4f7f\u7528 $1 \u6807\u8bc6\uff0c\u7b2c\u4e8c\u4e2a\u5b50\u5339\u914d\u5c06\u4f7f\u7528 $2 \u6807\u8bc6\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002\u8fd9\u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0c\u8fdb\u4e00\u6b65\u9610\u660e\u62ec\u53f7\u7684\u7528\u6cd5\u3002<\/p>\n<pre>\"I am looking for John and Jason\".replace(\/(w+)sands(w+)\/, '$2 and $1');\n\/\/ I am looking for Jason and John\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u5c06\u6807\u5fd7\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u7ed3\u5408\u4f7f\u7528<\/h2>\n<p>\u6b63\u5982\u6211\u5728\u7b80\u4ecb\u4e2d\u63d0\u5230\u7684\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e00\u4e2a\u66f4\u91cd\u8981\u7684\u529f\u80fd\u662f\u4f7f\u7528\u7279\u6b8a\u6807\u5fd7\u6765\u4fee\u6539\u641c\u7d22\u7684\u6267\u884c\u65b9\u5f0f\u3002\u8fd9\u4e9b\u6807\u5fd7\u662f\u53ef\u9009\u7684\uff0c\u4f46\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u6765\u6267\u884c\u8bf8\u5982\u5168\u5c40\u641c\u7d22\u6216\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u4e4b\u7c7b\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u8fd9\u4e9b\u662f\u56db\u4e2a\u5e38\u7528\u7684\u6807\u5fd7\uff0c\u7528\u4e8e\u66f4\u6539 JavaScript \u641c\u7d22\u6216\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u3002<\/p>\n<ul>\n<li> g\uff1a\u8be5\u6807\u5fd7\u5c06\u6267\u884c\u5168\u5c40\u641c\u7d22\uff0c\u800c\u4e0d\u662f\u5728\u7b2c\u4e00\u4e2a\u5339\u914d\u540e\u505c\u6b62\u3002<\/li>\n<li> i\uff1a\u6b64\u6807\u5fd7\u5c06\u6267\u884c\u641c\u7d22\uff0c\u800c\u4e0d\u68c0\u67e5\u5927\u5c0f\u5199\u662f\u5426\u5b8c\u5168\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5728\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u641c\u7d22\u4e2d\uff0cApple\u3001aPPLe \u548c apPLE \u7684\u5904\u7406\u65b9\u5f0f\u90fd\u662f\u76f8\u540c\u7684\u3002<\/li>\n<li> m\uff1a\u8be5\u6807\u5fd7\u5c06\u6267\u884c\u591a\u884c\u641c\u7d22\u3002<\/li>\n<li> y\uff1a\u6b64\u6807\u5fd7\u5c06\u5728 lastIndex \u5c5e\u6027\u6307\u793a\u7684\u7d22\u5f15\u4e2d\u67e5\u627e\u5339\u914d\u9879\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e0e\u6807\u5fd7\u4e00\u8d77\u4f7f\u7528\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<pre>\"I ate apples, you ate apples\".replace(\/apples\/, \"mangoes\");\n\/\/ \"I ate mangoes, you ate apples\"\n\n\"I ate apples, you ate apples\".replace(\/apples\/g, \"mangoes\");\n\/\/ \"I ate mangoes, you ate mangoes\"\n\n\"I ate apples, you ate APPLES\".replace(\/apples\/, \"mangoes\");\n\/\/ \"I ate mangoes, you ate APPLES\"\n\n\"I ate apples, you ate APPLES\".replace(\/apples\/gi, \"mangoes\");\n\/\/ \"I ate mangoes, you ate mangoes\"\n\n\nvar stickyRegex = \/apples\/y;\nstickyRegex.lastIndex = 3;\n\"I ate apples, you ate apples\".replace(stickyRegex, \"mangoes\");\n\/\/ \"I ate apples, you ate apples\"\n\nvar stickyRegex = \/apples\/y;\nstickyRegex.lastIndex = 6;\n\"I ate apples, you ate apples\".replace(stickyRegex, \"mangoes\");\n\/\/ \"I ate mangoes, you ate apples\"\n\nvar stickyRegex = \/apples\/y;\nstickyRegex.lastIndex = 8;\n\"I ate apples, you ate apples\".replace(stickyRegex, \"mangoes\");\n\/\/ \"I ate apples, you ate apples\"\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u6700\u7ec8\u60f3\u6cd5<\/h2>\n<p>\u672c\u6559\u7a0b\u7684\u76ee\u7684\u662f\u5411\u60a8\u4ecb\u7ecd JavaScript \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ca\u5176\u91cd\u8981\u6027\u3002\u6211\u4eec\u4ece\u57fa\u7840\u77e5\u8bc6\u5f00\u59cb\uff0c\u7136\u540e\u4ecb\u7ecd\u53cd\u659c\u6760\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u3002\u6211\u4eec\u8fd8\u5b66\u4e60\u4e86\u5982\u4f55\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u6a21\u5f0f\u4ee5\u53ca\u5982\u4f55\u8bb0\u4f4f\u6a21\u5f0f\u4e2d\u7684\u90e8\u5206\u5339\u914d\u4ee5\u4fbf\u4ee5\u540e\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5e38\u7528\u7684\u6807\u5fd7\uff0c\u5b83\u4eec\u4f7f\u6b63\u5219\u8868\u8fbe\u5f0f\u53d8\u5f97\u66f4\u52a0\u5f3a\u5927\u3002\u60a8\u53ef\u4ee5\u5728 MDN \u4e0a\u7684\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u4e86\u89e3\u6709\u5173\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u6211\u5728\u672c\u6559\u7a0b\u4e2d\u6f84\u6e05\u4efb\u4f55\u5185\u5bb9\uff0c\u8bf7\u968f\u65f6\u5728\u8bc4\u8bba\u4e2d\u544a\u8bc9\u6211\u3002<\/p>\n<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u521d\u5b66\u8005\u6307\u5357\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>\u6bcf\u4e2a\u4f7f\u7528 JavaScript \u7684\u4eba\u90fd\u5fc5\u987b\u5728\u67d0\u4e00\u65f6\u523b\u5904\u7406\u5b57\u7b26\u4e32\u3002\u6709\u65f6\uff0c\u60a8\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u53e6\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u51fa\u53bb\u3002\u5176\u4ed6\u65f6\u5019\uff0c\u60a8\u5fc5\u987b\u68c0\u67e5\u5b83\u5e76\u67e5\u770b\u5b83\u662f\u5426\u5305\u542b\u7279\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\u3002 \u7136\u800c\uff0c\u4e8b\u60c5\u5e76\u4e0d\u603b\u662f\u90a3\u4e48\u5bb9\u6613\u3002\u6709\u65f6\u60a8\u4e0d\u4f1a\u67e5\u627e\u7279\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u67e5\u627e\u9075\u5faa\u7279\u5b9a\u6a21\u5f0f\u7684\u4e00\u7ec4\u5b50\u5b57\u7b26\u4e32\u3002 \u5047\u8bbe\u60a8\u5fc5\u987b\u5c06\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u201cApples\u201d\u66ff\u6362\u4e3a\u201capples\u201d\u3002\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528 theMainString.replace(&#8220;Apples&#8221;, &#8220;apples&#8221;)\u3002\u597d\uff0c\u6613\u4e8e\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u73b0\u5728\u5047\u8bbe\u60a8\u8fd8\u5fc5\u987b\u5c06\u201cappLes\u201d\u66ff\u6362\u4e3a\u201capples\u201d\u3002\u540c\u6837\uff0c\u201cappLES\u201d\u4e5f\u5e94\u8be5\u53d8\u6210\u201capples\u201d\u3002\u57fa\u672c\u4e0a\uff0c\u201cApple\u201d\u7684\u6240\u6709\u5927\u5c0f\u5199\u53d8\u4f53\u90fd\u9700\u8981\u66f4\u6539\u4e3a\u201capple\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f20\u9012\u7b80\u5355\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u5c06\u4e0d\u518d\u5b9e\u7528\u6216\u9ad8\u6548\u3002 \u8fd9\u5c31\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6b66\u4e4b\u5730 &#8211; \u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6807\u5fd7 i \u5e76\u5b8c\u6210\u5b83\u3002\u4f7f\u7528\u8be5\u6807\u5fd7\u540e\uff0c\u539f\u59cb\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u201cApples\u201d\u3001\u201cAPPles\u201d\u3001\u201cApPlEs\u201d\u6216\u201cApples\u201d\u5e76\u4e0d\u91cd\u8981\u3002\u8be5\u5355\u8bcd\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u5c06\u66ff\u6362\u4e3a\u201capples\u201d\u3002 \u5c31\u50cf\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6807\u5fd7\u4e00\u6837\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u529f\u80fd\uff0c\u672c\u6559\u7a0b\u5c06\u4ecb\u7ecd\u8fd9\u4e9b\u529f\u80fd\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728 JavaScript \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f \u60a8\u5fc5\u987b\u4f7f\u7528\u7a0d\u5fae\u4e0d\u540c\u7684\u8bed\u6cd5\u6765\u6307\u793a\u4e0d\u540c String \u65b9\u6cd5\u5185\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4e0e\u7528\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u7b80\u5355\u5b57\u7b26\u4e32\u4e0d\u540c\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7531\u62ec\u5728\u659c\u6760\u4e4b\u95f4\u7684\u6a21\u5f0f\u7ec4\u6210\u3002\u60a8\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u7684\u4efb\u4f55\u6807\u5fd7\u90fd\u5c06\u9644\u52a0\u5728\u7b2c\u4e8c\u4e2a\u659c\u6760\u4e4b\u540e\u3002 \u56de\u5230\u524d\u9762\u7684\u793a\u4f8b\uff0c\u4e0b\u9762\u662f replace() \u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u7b80\u5355\u5b57\u7b26\u4e32\u7684\u6837\u5b50\u3002 &#8220;I ate Apples&#8221;.replace(&#8220;Apples&#8221;, &#8220;apples&#8221;); \/\/ I ate apples &#8220;I ate Apples&#8221;.replace(\/Apples\/i, &#8220;apples&#8221;); \/\/ I ate apples &#8220;I ate aPPles&#8221;.replace(&#8220;Apples&#8221;, &#8220;apples&#8221;); \/\/ I ate aPPles &#8220;I ate aPPles&#8221;.replace(\/Apples\/i, &#8220;apples&#8221;); \/\/ I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32377","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32377","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=32377"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32377\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}