简体   繁体   English

如何将字符串拆分为给定字符的数组(Javascript)

[英]How to split a string into an array at a given character (Javascript)

var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664.";

//Finished result should be:
result == ["10000.", "9409.", "13924.", "11025.", "10000.", "_.", "11025.", "13225.", "_.", "9801.", "12321.", "12321.", "11664."]

After each "."在每个“。”之后I want to split it and push it into an array.我想拆分它并将其推入一个数组。

You split it, and map over.你拆分它,然后 map 结束。 With every iteration you add an.每次迭代都添加一个。 to the end到最后

 var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664."; let result = stringToSplit.split(".").map(el => el + "."); console.log(result)

You could match the parts, instead of using split.您可以匹配零件,而不是使用拆分。

 var string = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664.", result = string.match(/[^.]+\./g); console.log(result);

 var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664."; var arr = stringToSplit.split(".").map(item => item+"."); console.log(arr);

split the string using .使用 . 拆分字符串. delimiter and then slice to remove the last empty space.分隔符,然后切片以删除最后一个空格。 Then use map to return the required array of elements然后使用map返回所需的元素数组

 var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664."; let newData = stringToSplit.split('.'); let val = newData.slice(0, newData.length - 1).map(item => `${item}.`) console.log(val)

you could use a lookbehind with .split你可以使用.splitlookbehind

 var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664."; let out = stringToSplit.split(/(?<=\.)/); console.log(out)

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

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