{"id":166115,"date":"2024-05-08T15:29:57","date_gmt":"2024-05-08T07:29:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/166115.html"},"modified":"2024-05-08T15:30:03","modified_gmt":"2024-05-08T07:30:03","slug":"javascript%e4%b8%adstring%e7%9a%84%e8%87%aa%e6%9c%89%e5%87%bd%e6%95%b0split%e6%80%8e%e4%b9%88%e5%88%86%e5%89%b2%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/166115.html","title":{"rendered":"JavaScript\u4e2dString\u7684\u81ea\u6709\u51fd\u6570split()\u600e\u4e48\u5206\u5272\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233127\/acf4de66-2aef-431e-8360-8af093a3fdb7.webp\" alt=\"JavaScript\u4e2dString\u7684\u81ea\u6709\u51fd\u6570split()\u600e\u4e48\u5206\u5272\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c<code>String<\/code>\u5bf9\u8c61\u7684\u81ea\u6709\u51fd\u6570<code>split()<\/code>\u7528\u4e8e\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5b57\u7b26\u4e32\u6570\u7ec4\u3002<strong>\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u7b2c\u4e00\u4e2a\u662f\u7528\u4f5c\u5206\u9694\u7b26\u7684\u5b57\u7b26\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7b2c\u4e8c\u4e2a\u662f\u53ef\u9009\u7684\uff0c\u6307\u5b9a\u8fd4\u56de\u6570\u7ec4\u7684\u6700\u5927\u957f\u5ea6<\/strong>\u3002<strong>\u6700\u5173\u952e\u7684\u529f\u80fd\u662f\uff0c<\/strong><code>split()<\/code><strong>\u80fd\u591f\u57fa\u4e8e\u6307\u5b9a\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u89e3\uff0c\u4ece\u800c\u4f7f\u5f97\u5b57\u7b26\u4e32\u5904\u7406\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5c55\u5f00\u63cf\u8ff0\u5176\u4e2d\u4e00\u70b9\uff0c<code>split()<\/code>\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5206\u9694\u7b26\u65f6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u548c\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u5206\u5272\u573a\u666f\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u6216\u8005\u6307\u5b9a\u4e00\u79cd\u6a21\u5f0f\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u5f3a\u5927\u7684\u529f\u80fd\u4f7f\u5f97<code>split()<\/code>\u5728\u5904\u7406\u590d\u6742\u6587\u672c\u6570\u636e\u65f6\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\uff0c\u4ece\u800c\u80fd\u6709\u6548\u5e94\u5bf9\u5404\u79cd\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>split()<\/code>\u57fa\u672c\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u6bd4\u5982\uff0c\u60f3\u8981\u6309\u7167\u9017\u53f7\u6765\u5206\u5272\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u53ea\u9700\u8981\u5c06\u9017\u53f7\u4f20\u9012\u7ed9<code>split()<\/code>\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const exampleStr = &quot;Apple, Banana, Cherry&quot;;<\/p>\n<p>const fruits = exampleStr.split(&quot;, &quot;);<\/p>\n<p>console.log(fruits); \/\/ \u8f93\u51fa: [&quot;Apple&quot;, &quot;Banana&quot;, &quot;Cherry&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>split()<\/code>\u6309\u7167\u9017\u53f7\u52a0\u7a7a\u683c\u7684\u65b9\u5f0f\u5206\u5272\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u7528\u4e8e\u5904\u7406\u7b80\u5355\u7684CSV(\u9017\u53f7\u5206\u9694\u503c)\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u5206\u5272\u6761\u4ef6\u65f6\uff0c<code>split()<\/code>\u51fd\u6570\u7684\u771f\u6b63\u5a01\u529b\u4f53\u73b0\u5728\u5b83\u80fd\u63a5\u53d7\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u8fd9\u4f7f\u5f97\u5206\u5272\u64cd\u4f5c\u53ef\u4ee5\u57fa\u4e8e\u6a21\u5f0f\u5339\u914d\u6765\u8fdb\u884c\uff0c\u5927\u5927\u589e\u5f3a\u4e86\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const complexStr = &quot;Apple1Banana2Cherry3&quot;;<\/p>\n<p>const result = complexStr.split(\/\\d\/); \/\/ \u4f7f\u7528\u6570\u5b57\u4f5c\u4e3a\u5206\u9694\u7b26<\/p>\n<p>console.log(result); \/\/ \u8f93\u51fa: [&quot;Apple&quot;, &quot;Banana&quot;, &quot;Cherry&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d<\/code>\uff0c\u5b83\u5339\u914d\u4efb\u4f55\u5355\u4e2a\u6570\u5b57\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u662f\u6839\u636e\u6570\u5b57\u8fdb\u884c\u5206\u5272\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u9650\u5b9a\u8fd4\u56de\u6570\u7ec4\u7684\u957f\u5ea6<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a<code>split()<\/code>\u51fd\u6570\u7684\u91cd\u8981\u7279\u6027\u662f\u53ef\u4ee5\u9650\u5b9a\u8fd4\u56de\u6570\u7ec4\u7684\u6700\u5927\u957f\u5ea6\u3002\u901a\u8fc7\u4f20\u9012\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff08\u4e00\u4e2a\u6574\u6570\uff09\uff0c\u53ef\u4ee5\u63a7\u5236\u8fd4\u56de\u7684\u6570\u7ec4\u4e2d\u6700\u591a\u5305\u542b\u591a\u5c11\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const someString = &quot;One, Two, Three, Four, Five&quot;;<\/p>\n<p>const limitedArray = someString.split(&quot;, &quot;, 3);<\/p>\n<p>console.log(limitedArray); \/\/ \u8f93\u51fa: [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u529f\u80fd\u5c24\u5176\u5728\u4f60\u53ea\u9700\u8981\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u8282\u7701\u5185\u5b58\uff0c\u5e76\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p><code>split()<\/code>\u7684\u4f7f\u7528\u8fdc\u4e0d\u9650\u4e8e\u4ee5\u4e0a\u63cf\u8ff0\u7684\u8fd9\u4e9b\u573a\u666f\u3002\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u6781\u5176\u590d\u6742\u7684\u5206\u5272\u6a21\u5f0f\uff0c\u6ee1\u8db3\u51e0\u4e4e\u4efb\u4f55\u5b57\u7b26\u4e32\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u5206\u5272\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u53ea\u6709\u5f53\u5206\u9694\u7b26\u524d\u9762\u4e0d\u662f\u53cd\u659c\u6760\uff08\\\uff09\u65f6\u624d\u8fdb\u884c\u5206\u5272\uff0c\u8fd9\u79cd\u590d\u6742\u7684\u6761\u4ef6\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u52a0\u4ee5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const advancedStr = &quot;Apple\\\\, Banana, Cherry&quot;;<\/p>\n<p>const advancedResult = advancedStr.split(\/(?&lt;!\\\\), \/);<\/p>\n<p>console.log(advancedResult); \/\/ \u8f93\u51fa: [&quot;Apple\\\\, Banana&quot;, &quot;Cherry&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u96f6\u5bbd\u8d1f\u56de\u987e\u540e\u53d1\u65ad\u8a00\uff08<code>(?&lt;!\\\\)<\/code>\uff09\u6765\u786e\u4fdd\u5206\u9694\u7b26<code>,<\/code>\u524d\u9762\u4e0d\u662f\u53cd\u659c\u6760<code><\/code>\u3002\u8fd9\u6837\uff0c\u5373\u4fbf\u5b57\u7b26\u4e32\u4e2d\u6709\u9017\u53f7\uff0c\u4f46\u5982\u679c\u5b83\u524d\u9762\u6709\u53cd\u659c\u6760\uff0c\u8be5\u9017\u53f7\u4e0d\u4f1a\u88ab\u7528\u4f5c\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p><code>split()<\/code>\u662fJavaScript\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u7b80\u5355\u7684\u6587\u672c\u5206\u5272\uff0c\u8fd8\u662f\u89e3\u51b3\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u95ee\u9898\uff0c<code>split()<\/code>\u90fd\u80fd\u63d0\u4f9b\u7075\u6d3b\u3001\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u638c\u63e1\u5b83\u7684\u7528\u6cd5\u5bf9\u4e8e\u6bcf\u4e2aJavaScript\u7a0b\u5e8f\u5458\u6765\u8bf4\u90fd\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u901a\u8fc7\u5408\u7406\u5229\u7528<code>split()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u5b57\u7b26\u4e32\u5904\u7406\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528JavaScript\u4e2dString\u7684split()\u51fd\u6570\u6765\u5206\u5272\u5b57\u7b26\u4e32\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528split()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u901a\u8fc7\u6307\u5b9a\u5206\u9694\u7b26\u4f5c\u4e3a\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u4e00\u4e2a\u53e5\u5b50\u6839\u636e\u7a7a\u683c\u5206\u5272\u6210\u5355\u8bcd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">var sentence = &quot;This is a sentence&quot;;\nvar words = sentence.split(&quot; &quot;);\nconsole.log(words); \/\/ \u8f93\u51fa\uff1a[&quot;This&quot;, &quot;is&quot;, &quot;a&quot;, &quot;sentence&quot;]\n<\/code><\/pre>\n<p><strong>2. split()\u51fd\u6570\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u5417\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0csplit()\u51fd\u6570\u652f\u6301\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u6307\u5b9a\u5206\u9694\u7b26\u3002\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u4f7f\u7528\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u8981\u6839\u636e\u9017\u53f7\u6216\u5206\u53f7\u6765\u5206\u5272\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u503c\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">var values = &quot;1,2,3;4,5,6&quot;;\nvar array = values.split(\/[,;]\/);\nconsole.log(array); \/\/ \u8f93\u51fa\uff1a[&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;]\n<\/code><\/pre>\n<p><strong>3. split()\u51fd\u6570\u8fd8\u53ef\u4ee5\u9650\u5236\u5206\u5272\u7684\u6b21\u6570\u5417\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0csplit()\u51fd\u6570\u8fd8\u652f\u6301\u53ef\u9009\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u7528\u4e8e\u9650\u5236\u5206\u5272\u7684\u6b21\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csplit()\u51fd\u6570\u5c06\u5206\u5272\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u6570\u5b57\u6765\u9650\u5236\u5206\u5272\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff0c\u8981\u5c06\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u503c\u7684\u5b57\u7b26\u4e32\u4ec5\u5206\u5272\u6210\u524d\u4e24\u4e2a\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">var values = &quot;1,2,3,4,5,6&quot;;\nvar array = values.split(&quot;,&quot;, 2);\nconsole.log(array); \/\/ \u8f93\u51fa\uff1a[&quot;1&quot;, &quot;2&quot;]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u7b26\u4e32\u5c06\u53ea\u88ab\u5206\u5272\u6210\u4e24\u90e8\u5206\uff0c\u8d85\u8fc7\u6307\u5b9a\u6b21\u6570\u7684\u5206\u5272\u5c06\u88ab\u5ffd\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\uff0cString\u5bf9\u8c61\u7684\u81ea\u6709\u51fd\u6570split()\u7528\u4e8e\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5 [&hellip;]","protected":false},"author":3,"featured_media":166125,"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\/166115"}],"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=166115"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166115\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/166125"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=166115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=166115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=166115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}