{"id":11168,"date":"2024-08-27T08:43:50","date_gmt":"2024-08-27T08:43:50","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11168"},"modified":"2024-08-27T08:43:50","modified_gmt":"2024-08-27T08:43:50","slug":"how-to-convert-a-string-to-an-array-with-javascript-typescript","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/javascript\/how-to-convert-a-string-to-an-array-with-javascript-typescript","title":{"rendered":"How to Convert a String to an Array with JavaScript\/TypeScript"},"content":{"rendered":"<p>If you want to split the string into an array based on the pattern of a number followed by a dot and space, you can use a regular expression (regex) in JavaScript to achieve that.<br \/>\nHere&#8217;s an example of using regex to split the string and achieve the desired output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst inputString = '1. Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum';\r\nconst outputArray = inputString.split(\/\\d+\\.\\s\/).filter(item =&gt; item !== '');\r\nconsole.log(outputArray);\r\n\/\/ Output: [ '1. Lorem Ipsum.', '2. Lorem Ipsum.', '3. Lorem Ipsum.' ]\r\n<\/pre>\n<h3>Explanation:<\/h3>\n<p><strong>split(\/\\d+\\.\\s\/)<\/strong> uses a regular expression as the argument to <strong>split()<\/strong> instead of a simple string. <strong>\/\\d+\\.\\s\/ is a regex pattern<\/strong> that matches one or more digits <strong>(\\d+)<\/strong>, followed by a dot (\\.), and a space (\\s).<\/p>\n<p>The split() method separates the string into an array based on this regex pattern.<br \/>\n<strong>filter(item => item !== &#8221;)<\/strong> is used to remove any empty strings from the resulting array, which might occur due to splitting the string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to split the string into an array based on the pattern of a number followed by a dot and space, you can use a regular expression (regex) in JavaScript to achieve that. Here&#8217;s an example of using regex to split the string and achieve the desired output: const inputString = &#8216;1. Lorem [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11160,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-11168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11168"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11168"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11168\/revisions"}],"predecessor-version":[{"id":11170,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11168\/revisions\/11170"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11160"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}