简体   繁体   English

避免在第一个字符串的数组上拆分第一个空格

[英]avoiding the first space on split the array of first string

how can i split the below string into a required array by avoiding the first space of the first string. 我如何通过避免第一个字符串的第一个空格将以下字符串拆分为所需的数组。

below is the sample snippet, i tried to split it with space but since first item 1 itself having a space. 下面是示例代码段,我尝试将其与空格分开,但由于第一项1本身具有空格。 How can i avoid it and take as a single element. 我如何才能避免这种情况并将其作为一个要素。

 const data = "Item 1 10 200" // required format [item 1, 10, 200] // what i tried is const implemented = data.split(" ") console.log(implemented) 

Looks like the "space" between items from your question is a tab 看来问题中项目之间的“空格”是一个标签

 console.log( "Item 1 10 200".split('\\t') ) //['Item 1', '10', '200'] 

In your given example, the delimiter is \\t (horizontal tab); 在给定的示例中,定界符为\\ t(水平制表符); so you can do like so, 所以你可以这样

 const data = "Item 1 10 200" const implemented = data.split(/\\t/) console.log(implemented) 

You can split() by \\t 您可以用\\ t split()

 let data = "Item 1 10 200" data = data.split("\\t"); console.log(data); 

Your string contains tabs instead of space so: 您的字符串包含制表符而不是空格,因此:

 const data = "Item 1 10 200" const implemented = data.split("\\t") console.log(implemented) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM