{"id":169227,"date":"2024-05-08T16:41:27","date_gmt":"2024-05-08T08:41:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169227.html"},"modified":"2024-05-08T16:41:31","modified_gmt":"2024-05-08T08:41:31","slug":"javascript-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%ad%a3%e5%88%99%e6%9b%bf%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/169227.html","title":{"rendered":"javascript \u5982\u4f55\u5b9e\u73b0\u6b63\u5219\u66ff\u6362"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035651\/6d280d5b-cfe7-4963-9ead-fc007bb0f5e9.webp\" alt=\"javascript \u5982\u4f55\u5b9e\u73b0\u6b63\u5219\u66ff\u6362\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\u5b9e\u73b0\u6b63\u5219\u66ff\u6362\u4e3b\u8981\u901a\u8fc7\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684<code>replace()<\/code>\u65b9\u6cd5\u6765\u5b8c\u6210\uff0c<strong>\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u3001<strong>\u66ff\u6362\u503c<\/strong>\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u662f\u901a\u8fc7\u66ff\u6362\u5339\u914d\u7684\u5b50\u4e32\u83b7\u5f97\u7684\u3002\u82e5\u8981\u8fdb\u884c\u5168\u5c40\u66ff\u6362\uff0c\u9700\u8981\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528<code>g<\/code>\u6807\u5fd7\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u66ff\u6362\u53c2\u6570\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u66ff\u6362\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u66ff\u6362\u5b57\u7b26\u4e32\u65f6<\/strong>\uff0c\u53ef\u4ee5\u5728\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u7279\u6b8a\u7684\u5360\u4f4d\u7b26\uff0c\u5982<code>$1<\/code>\u3001<code>$2<\/code>\u7b49\uff0c\u4ee3\u8868\u7b2c\u4e00\u4e2a\u3001\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u5339\u914d\u7684\u5185\u5bb9\uff0c\u800c<code>$&amp;<\/code>\u8868\u793a\u6574\u4e2a\u5339\u914d\u7684\u5b50\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u73b0\u5728\u8ba9\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u5982\u4f55\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u8fdb\u884c\u6b63\u5219\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u7840\u66ff\u6362\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>A. \u7b80\u5355\u7684\u5b57\u7b26\u4e32\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u662f\u6700\u57fa\u7840\u7684\u7528\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6587\u672c\u66ff\u6362\u4e3a\u53e6\u4e00\u6bb5\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;Hello, World!&quot;;<\/p>\n<p>let result = text.replace(&quot;World&quot;, &quot;JavaScript&quot;);<\/p>\n<p>console.log(result); \/\/ &quot;Hello, JavaScript!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>B. \u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u7075\u6d3b\u7684\u66ff\u6362\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;The sky is blue, and the grass is green.&quot;;<\/p>\n<p>let regex = \/blue|green\/g;<\/p>\n<p>let result = text.replace(regex, &quot;red&quot;);<\/p>\n<p>console.log(result); \/\/ &quot;The sky is red, and the grass is red.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u9ad8\u7ea7\u66ff\u6362\u6280\u5de7<\/h3>\n<\/p>\n<p><h4>A. \u4f7f\u7528\u6355\u83b7\u7ec4<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u6355\u83b7\u7ec4\uff0c\u4f60\u53ef\u4ee5\u5728\u66ff\u6362\u4e2d\u5f15\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;John Smith&quot;;<\/p>\n<p>let regex = \/(\\w+) (\\w+)\/;<\/p>\n<p>let result = text.replace(regex, &#039;$2, $1&#039;);<\/p>\n<p>console.log(result); \/\/ &quot;Smith, John&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>B. \u4f7f\u7528\u51fd\u6570\u8fdb\u884c\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u5f53\u66ff\u6362\u66f4\u52a0\u590d\u6742\u65f6\uff0c\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u51fd\u6570\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;John Smith&quot;;<\/p>\n<p>let regex = \/(\\w+) (\\w+)\/;<\/p>\n<p>let result = text.replace(regex, function(match, p1, p2) {<\/p>\n<p>    return p2.toUpperCase() + &#039;, &#039; + p1;<\/p>\n<p>});<\/p>\n<p>console.log(result); \/\/ &quot;SMITH, John&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u8ba9\u6211\u4eec\u6765\u770b\u51e0\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u4ee5\u5c55\u793a<code>replace()<\/code>\u65b9\u6cd5\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>A. \u683c\u5f0f\u5316\u65e5\u671f<\/h4>\n<\/p>\n<p><p>\u60f3\u8c61\u4e00\u4e0b\u6211\u4eec\u9700\u8981\u5c06&quot;YYYY-MM-DD&quot;\u683c\u5f0f\u7684\u65e5\u671f\u8f6c\u6362\u4e3a&quot;DD\/MM\/YYYY&quot;\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let date = &quot;2023-04-01&quot;;<\/p>\n<p>let regex = \/(\\d{4})-(\\d{2})-(\\d{2})\/;<\/p>\n<p>let result = date.replace(regex, &#039;$3\/$2\/$1&#039;);<\/p>\n<p>console.log(result); \/\/ &quot;01\/04\/2023&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>B. \u79fb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684HTML\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4ece\u7f51\u9875\u4e2d\u6293\u53d6\u7684\u6587\u672c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u79fb\u9664\u6240\u6709\u7684HTML\u6807\u8bb0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let html = &quot;&lt;div&gt;Hello &lt;b&gt;World&lt;\/b&gt;!&lt;\/div&gt;&quot;;<\/p>\n<p>let regex = \/&lt;[^&gt;]+&gt;\/g;<\/p>\n<p>let result = html.replace(regex, &quot;&quot;);<\/p>\n<p>console.log(result); \/\/ &quot;Hello World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7279\u6b8a\u573a\u666f\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6700\u540e\u8ba9\u6211\u4eec\u770b\u4e00\u4e9b\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>A. \u66ff\u6362\u7279\u6b8a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u9700\u8981\u66ff\u6362\u7684\u6587\u672c\u4e2d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u8fd9\u4e9b\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;One plus one equals two: 1 + 1 = 2&quot;;<\/p>\n<p>let regex = \/\\+\/g; \/\/ \u52a0\u53f7\u9700\u8981\u8f6c\u4e49\uff0c\u56e0\u4e3a\u5b83\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u6709\u7279\u6b8a\u610f\u4e49<\/p>\n<p>let result = text.replace(regex, &quot;plus&quot;);<\/p>\n<p>console.log(result); \/\/ &quot;One plus one equals two: 1 plus 1 = 2&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>B. \u52a8\u6001\u751f\u6210\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u57fa\u4e8e\u67d0\u4e9b\u53d8\u91cf\u52a8\u6001\u751f\u6210\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let searchString = &quot;blue&quot;;<\/p>\n<p>let flags = &quot;gi&quot;; \/\/ global, case-insensitive<\/p>\n<p>let regex = new RegExp(searchString, flags);<\/p>\n<p>let text = &quot;Blue, blue, electric blue&quot;;<\/p>\n<p>let result = text.replace(regex, &quot;red&quot;);<\/p>\n<p>console.log(result); \/\/ &quot;red, red, electric red&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u786e\u5207\u7684\u7406\u89e3\u548c\u6280\u5de7\uff0cJavaScript\u4e2d\u7684\u6b63\u5219\u66ff\u6362\u53ef\u4ee5\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u9002\u5e94\u5404\u79cd\u5b57\u7b26\u4e32\u66ff\u6362\u7684\u9700\u6c42\u3002\u5173\u952e\u662f\u8981\u719f\u7ec3\u638c\u63e1\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5199\u6cd5\u548cJavaScript\u7684<code>replace()<\/code>\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\uff1f<\/strong><br \/>\n\u4f7f\u7528 JavaScript \u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u5b57\u7b26\u4e32\u66ff\u6362\u529f\u80fd\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\uff0c\u6307\u5b9a\u8981\u5339\u914d\u7684\u6a21\u5f0f\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>replace()<\/code> \u65b9\u6cd5\u5c06\u5339\u914d\u7684\u6a21\u5f0f\u66ff\u6362\u4e3a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u5e0c\u671b\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u66ff\u6362\u4e3a\u661f\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-javascript\">let str = &quot;Hello 123 World 456&quot;;\nlet newStr = str.replace(\/\\d\/g, &quot;*&quot;);\nconsole.log(newStr); \/\/ \u8f93\u51fa\uff1aHello <strong>* World <\/strong>*\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>\/\\d\/g<\/code> \u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5b83\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002<code>replace()<\/code> \u65b9\u6cd5\u5c06\u5339\u914d\u5230\u7684\u6570\u5b57\u66ff\u6362\u4e3a\u661f\u53f7\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9 <code>newStr<\/code> \u53d8\u91cf\u3002<\/p>\n<p><strong>2. JavaScript \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u53ef\u4ee5\u5e26\u6709\u56de\u8c03\u51fd\u6570\u5417\uff1f<\/strong><br \/>\n\u662f\u7684\uff0cJavaScript \u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u65b9\u6cd5 <code>replace()<\/code> \u8fd8\u652f\u6301\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u66ff\u6362\u64cd\u4f5c\u3002\u901a\u8fc7\u5728\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u5339\u914d\u7684\u5185\u5bb9\u52a8\u6001\u751f\u6210\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-javascript\">let str = &quot;Hello World&quot;;\nlet newStr = str.replace(\/(\\w+)\/g, function(match, p1){\n  return p1.split(&#039;&#039;).reverse().join(&#039;&#039;);\n});\nconsole.log(newStr); \/\/ \u8f93\u51fa\uff1aolleH dlroW\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f <code>\/(\\w+)\/g<\/code> \u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u3002\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u6211\u4eec\u5c06\u6bcf\u4e2a\u5339\u914d\u7684\u5355\u8bcd\u8fdb\u884c\u53cd\u8f6c\uff0c\u5e76\u5c06\u5176\u8fd4\u56de\u4f5c\u4e3a\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\uff1f<\/strong><br \/>\nJavaScript \u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u8fdb\u884c\u66ff\u6362\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>\/g<\/code> \u4fee\u9970\u7b26\uff0c\u5b83\u53ef\u4ee5\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5339\u914d\u9879\u3002\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5c06\u6240\u6709\u9017\u53f7\u66ff\u6362\u4e3a\u53e5\u53f7\uff1a<\/p>\n<pre><code class=\"language-javascript\">let str = &quot;Hello, World, How, Are, You&quot;;\nlet newStr = str.replace(\/,\/g, &quot;.&quot;);\nconsole.log(newStr); \/\/ \u8f93\u51fa\uff1aHello. World. How. Are. You\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f <code>\/,\/g<\/code> \u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u9017\u53f7\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u53e5\u53f7\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u5c06\u6307\u5b9a\u5b57\u7b26\u66ff\u6362\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\u5b9e\u73b0\u6b63\u5219\u66ff\u6362\u4e3b\u8981\u901a\u8fc7\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684replace()\u65b9\u6cd5\u6765\u5b8c\u6210\uff0c\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":169231,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169227"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=169227"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169227\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/169231"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=169227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=169227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=169227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}