{"id":168218,"date":"2024-05-08T16:19:21","date_gmt":"2024-05-08T08:19:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/168218.html"},"modified":"2024-05-08T16:19:26","modified_gmt":"2024-05-08T08:19:26","slug":"javascript-%e4%b8%ad-split-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/168218.html","title":{"rendered":"Javascript \u4e2d split \u51fd\u6570\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034402\/2c8b6e00-9483-47f8-b087-b372014955ac.webp\" alt=\"Javascript \u4e2d split \u51fd\u6570\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p><strong>JavaScript\u4e2d\u7684split\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u5272\u6210\u6570\u7ec4\u3001\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u6570\u636e\u3001\u65b9\u4fbf\u5feb\u901f\u63d0\u53d6\u4fe1\u606f\u3002<\/strong> \u5176\u4e2d\uff0c\u6838\u5fc3\u7528\u6cd5\u662f\u8c03\u7528\u5b57\u7b26\u4e32\u5b9e\u4f8b\u7684split\u65b9\u6cd5\uff0c\u4f20\u5165\u4e00\u4e2a\u5206\u9694\u7b26\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u53e6\u5916\uff0csplit\u51fd\u6570\u8fd8\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u53ef\u9009\u7684\u201c\u9650\u5236\u8fd4\u56de\u6570\u7ec4\u5927\u5c0f\u201d\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\u8be6\u7ec6\u6765\u8bf4\uff0csplit\u51fd\u6570\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u4ece\u4e00\u4e2a\u8f83\u957f\u7684\u5b57\u7b26\u4e32\u8f7b\u677e\u62bd\u79bb\u51fa\u6240\u9700\u7684\u90e8\u5206\uff0c<strong>\u4e3a\u6587\u672c\u5904\u7406\u5e26\u6765\u6781\u5927\u4fbf\u5229<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<strong>SPLIT\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>split\u51fd\u6570\u7684\u57fa\u672c\u4f7f\u7528\u6a21\u5f0f\u975e\u5e38\u76f4\u63a5\u3002\u5f53\u4f60\u6709\u4e00\u4e2a\u9700\u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u4f60\u4ec5\u4ec5\u9700\u8981\u786e\u5b9a\u4e00\u4e2a\u5206\u754c\u70b9\uff0c\u8fd9\u4e2a\u5206\u754c\u70b9\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u7b26\u3001\u4e00\u7cfb\u5217\u5b57\u7b26\u6216\u662f\u4e00\u4e2a\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684split\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;apple,orange,banana,kiwi&quot;;<\/p>\n<p>let result = text.split(&quot;,&quot;); \/\/ \u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;apple,orange,banana,kiwi&quot;<\/code>\u88ab\u9017\u53f7\u5206\u9694\uff0c\u5e76\u8fd4\u56de\u4e86\u4e00\u4e2a\u5305\u542b\u56db\u4e2a\u6c34\u679c\u540d\u79f0\u7684\u6570\u7ec4\uff1a<code>[&quot;apple&quot;, &quot;orange&quot;, &quot;banana&quot;, &quot;kiwi&quot;]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001<strong>\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u5206\u9694\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528split\u51fd\u6570\u65f6\u80fd\u591f\u6307\u5b9a\u591a\u79cd\u7c7b\u578b\u7684\u5206\u9694\u7b26\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;Hello World. How are you?&quot;;<\/p>\n<p>let bySpaces = text.split(&quot; &quot;); \/\/ \u6309\u7a7a\u683c\u5206\u9694<\/p>\n<p>let byDots = text.split(&quot;.&quot;); \/\/ \u6309\u53e5\u70b9\u5206\u9694<\/p>\n<p>let byRegExp = text.split(\/\\b\/); \/\/ \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u9694\u5355\u8bcd\u8fb9\u754c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001<strong>\u9650\u5236\u8fd4\u56de\u6570\u7ec4\u7684\u5927\u5c0f<\/strong><\/p>\n<\/p>\n<p><p>split\u51fd\u6570\u8fd8\u5141\u8bb8\u4f60\u9650\u5b9a\u8f93\u51fa\u6570\u7ec4\u7684\u6700\u5927\u957f\u5ea6\u3002\u901a\u8fc7\u4f20\u5165\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u8bbe\u5b9a\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u4e0a\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;apple,orange,banana,kiwi&quot;;<\/p>\n<p>let limitResult = text.split(&quot;,&quot;, 2); \/\/ \u7ed3\u679c\u662f[&#039;apple&#039;, &#039;orange&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u867d\u7136\u539f\u5b57\u7b26\u4e32\u7531\u56db\u4e2a\u9879\u76ee\u6784\u6210\uff0c\u4f46\u662f\u56e0\u4e3a\u6307\u5b9a\u4e86\u53c2\u6570\u201c2\u201d\uff0c\u7ed3\u679c\u6570\u7ec4\u4e2d\u4ec5\u5305\u542b\u524d\u4e24\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001<strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5206\u9694\u7b26<\/strong><\/p>\n<\/p>\n<p><p>split\u51fd\u6570\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u8fd9\u6837\u53ef\u4ee5\u6267\u884c\u66f4\u4e3a\u590d\u6742\u7684\u5206\u5272\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;Words, words. Words! Words? -Words-&quot;;<\/p>\n<p>let wordsArray = text.split(\/[ ,.!?\\-]+\/);<\/p>\n<p>\/\/ \u7ed3\u679c\u6570\u7ec4\u5c06\u5305\u542b\u4e0d\u542b\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u7684\u5355\u8bcd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>[ ,.!?\\-]+<\/code>\u6307\u5b9a\u4e86\u4e00\u4e2a\u7531\u9017\u53f7\u3001\u7a7a\u683c\u3001\u611f\u53f9\u53f7\u3001\u95ee\u53f7\u4ee5\u53ca\u8fde\u5b57\u7b26\u7ec4\u6210\u7684\u5b57\u7b26\u96c6\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5176\u4e2d\u201c+\u201d\u8868\u793a\u5339\u914d\u5206\u9694\u7b26\u4e00\u6b21\u6216\u591a\u6b21\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001<strong>\u4fdd\u7559\u5206\u9694\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csplit\u4f1a\u4e22\u5f03\u5206\u9694\u7b26\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4fdd\u7559\u5206\u9694\u7b26\uff0c\u8fd9\u4e5f\u80fd\u591f\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6355\u83b7\u62ec\u53f7\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;Hello World. How are you?&quot;;<\/p>\n<p>let parts = text.split(\/([ ,.]+)\/);<\/p>\n<p>\/\/ \u7ed3\u679c\u6570\u7ec4\u4e2d\u5c06\u5305\u542b\u5206\u9694\u7b26\uff0c\u4f8b\u5982: [&quot;Hello&quot;, &quot; &quot;, &quot;World&quot;, &quot;. &quot;, &quot;How&quot;, &quot; &quot;, &quot;are&quot;, &quot; &quot;, &quot;you&quot;, &quot;?&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001<strong>\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/strong><\/p>\n<\/p>\n<p><p>split\u65b9\u6cd5\u4e5f\u9700\u8981\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u5982\u5f53\u8fde\u7eed\u51fa\u73b0\u5206\u9694\u7b26\u6216\u5b57\u7b26\u4e32\u5f00\u5934\u3001\u672b\u5c3e\u6709\u5206\u9694\u7b26\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let text = &quot;,apple,orange,,banana,kiwi,&quot;;<\/p>\n<p>let multipleSeparated = text.split(&quot;,&quot;); \/\/ [&quot;&quot;, &quot;apple&quot;, &quot;orange&quot;, &quot;&quot;, &quot;banana&quot;, &quot;kiwi&quot;, &quot;&quot;]<\/p>\n<p>let trimmedResult = text.split(\/,+\/); \/\/ [&quot;apple&quot;, &quot;orange&quot;, &quot;banana&quot;, &quot;kiwi&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u793a\u4f8b\u5c55\u73b0\u4e86\u5982\u4f55\u533a\u5206\u8fde\u7eed\u5206\u9694\u7b26\u5bfc\u81f4\u7684\u7a7a\u5b57\u7b26\u4e32\u95ee\u9898\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664\u8fd9\u4e9b\u7a7a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001<strong>\u5c0f\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0csplit\u51fd\u6570\u662f\u5904\u7406\u548c\u8f6c\u6362\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u5b9d\u8d35\u5de5\u5177\u3002\u5b83\u7684\u7075\u6d3b\u6027\u4f7f\u5f97\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u5feb\u901f\u5730\u4ece\u590d\u6742\u7684\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002\u4e0d\u7ba1\u662f\u9762\u5411\u521d\u5b66\u8005\u8fd8\u662f\u9ad8\u7ea7\u7f16\u7a0b\u4efb\u52a1\uff0csplit\u90fd\u662fJavaScript\u7f16\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u8fd0\u7528split\u51fd\u6570\u80fd\u591f\u5728\u89e3\u6790CSV\u3001\u65e5\u5fd7\u6587\u4ef6\u3001URI\u53c2\u6570\u7b49\u591a\u79cd\u6570\u636e\u7c7b\u578b\u4e2d\u5927\u653e\u5f02\u5f69\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\uff1asplit\u51fd\u6570\u5728Javascript\u4e2d\u662f\u5982\u4f55\u4f7f\u7528\u7684\uff1f<\/strong><br \/>\n\u7b54\uff1asplit\u51fd\u6570\u662f\u7528\u6765\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u8fdb\u884c\u5206\u5272\u6210\u4e00\u4e2a\u6570\u7ec4\u7684\u65b9\u6cd5\u3002\u5728Javascript\u4e2d\uff0c\u4f7f\u7528split\u51fd\u6570\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5bf9\u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\u8c03\u7528split()\u65b9\u6cd5\uff0c\u5e76\u4f20\u5165\u8981\u7528\u6765\u5206\u5272\u7684\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u5373\u53ef\u3002\u4f8b\u5982\uff1a**<\/p>\n<pre><code>var str = &quot;apple, banana, orange&quot;;\nvar fruits = str.split(&quot;, &quot;);\nconsole.log(fruits); \/\/ [&#039;apple&#039;, &#039;banana&#039;, &#039;orange&#039;]\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5728\u8c03\u7528split()\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u4e2a\u5b57\u7b26\u6216\u591a\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5982\u679c\u4e0d\u4f20\u5165\u4efb\u4f55\u53c2\u6570\uff0c\u5219\u4f1a\u5c06\u6574\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u3002<\/p>\n<p><strong>\u95ee\uff1asplit\u51fd\u6570\u53ef\u4ee5\u6307\u5b9a\u5206\u5272\u6210\u51e0\u4e2a\u90e8\u5206\u5417\uff1f<\/strong><br \/>\n\u7b54\uff1a\u662f\u7684\uff0csplit\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6765\u6307\u5b9a\u5206\u5272\u6210\u591a\u5c11\u4e2a\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u53ef\u4ee5\u4f20\u51652\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\u5982\u679c\u60f3\u8981\u5206\u5272\u6210\u66f4\u591a\u7684\u90e8\u5206\uff0c\u53ea\u9700\u8981\u4f20\u5165\u60f3\u8981\u7684\u6570\u91cf\u5373\u53ef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>var str = &quot;apple, banana, orange&quot;;\nvar fruits = str.split(&quot;, &quot;, 2);\nconsole.log(fruits); \/\/ [&#039;apple&#039;, &#039;banana&#039;]\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5982\u679c\u5206\u5272\u7684\u6570\u91cf\u8d85\u8fc7\u4e86\u5b57\u7b26\u4e32\u4e2d\u7684\u5b9e\u9645\u90e8\u5206\u6570\u91cf\uff0csplit\u51fd\u6570\u4f1a\u8fd4\u56de\u6574\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>\u95ee\uff1asplit\u51fd\u6570\u5728\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\u6709\u4ec0\u4e48\u7279\u6b8a\u4e4b\u5904\uff1f<\/strong><br \/>\n\u7b54\uff1asplit\u51fd\u6570\u5728\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\u6709\u4e00\u4e9b\u7279\u6b8a\u4e4b\u5904\u3002\u800c\u4e0d\u662f\u5c06\u5206\u9694\u7b26\u4f5c\u4e3a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5206\u9694\u7b26\u4f20\u9012\u7ed9split\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u66f4\u590d\u6742\u7684\u6a21\u5f0f\u6765\u8fdb\u884c\u5206\u5272\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u6839\u636e\u9017\u53f7\u6216\u7a7a\u683c\u6765\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>\/[, ]\/<\/code>\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>var str = &quot;apple, banana orange&quot;;\nvar fruits = str.split(\/[, ]\/);\nconsole.log(fruits); \/\/ [&#039;apple&#039;, &#039;banana&#039;, &#039;orange&#039;]\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5206\u9694\u7b26\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u659c\u6760<code>\/<\/code>\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u5305\u88f9\u8d77\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u4e2d\u7684split\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u5272\u6210\u6570\u7ec4\u3001\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u6570\u636e\u3001\u65b9\u4fbf\u5feb\u901f\u63d0 [&hellip;]","protected":false},"author":3,"featured_media":168224,"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\/168218"}],"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=168218"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/168224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=168218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=168218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=168218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}