简体   繁体   English

我需要将重复单词上的长字符串拆分为数组

[英]I need to split a long string on a repeating word into an array

Trying to use Javascript, I have a really long string and I need to split it into array using a word that is repeated through the string. 尝试使用Javascript,我有一个很长的字符串,我需要使用在字符串中重复的单词将其拆分为数组。

Example: 例:

long_string = "THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074| THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074| THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|"

I've tried split and match but it always leaves out the THS& 我尝试过拆分和匹配,但始终忽略了THS&

split_string = [];
split_string = long_string.split(/THS&/);
console.log(split_string);


Into an array: 放入数组:

[THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074|, THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074|, THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|]

But what I get is something like 但是我得到的是这样的

[| Willam | Baker | 1234 Corker St| Jacksonville, TX 75074|, Steve | James | 4312 Corker St | Jacksonville, TX 75074|, | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|]

Whatever you match in a split (like THS& ) is not included in the result. 无论您在split匹配的内容(例如THS& )都不会包含在结果中。 The solution is to use a look-ahead , which does not actually capture the string: 解决方案是使用look-ahead ,它实际上不会捕获字符串:

 var long_string = "THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074| THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074| THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|" var split_string = long_string.split(/(?=THS&)/); console.log(split_string); 

The split() method doesn't include the separator inside the resulting substrings. split()方法在结果子字符串中不包含分隔符。 If your separator is always going to be the same, consider concatenating it at the beginning of each substring. 如果分隔符总是相同,请考虑在每个子字符串的开头将其串联。 (You could iterate through split_string and add concatenate "THS& " at the beginning of each string of the array) (您可以遍历split_string并在数组的每个字符串的开头添加连接"THS& "

The separator won't be included in the results so you'll have to add it back in. Also, the first item will be empty so use Array.prototype.shift() to strip it from the resulting array. 分隔符将不会包含在结果中,因此您必须将其重新添加。而且,第一项将为空,因此请使用Array.prototype.shift()从结果数组中剥离它。 EG: 例如:

 var long_string = "THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074| THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074| THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|"; var split_string = long_string.split("THS&").map(function(item) { return "THS&"+item; }); split_string.shift(); console.log(split_string); /* [ "THS& | Willam | Baker | 1234 Corker St| Jacksonville, TX 75074| ", "THS& Steve | James | 4312 Corker St | Jacksonville, TX 75074| ", "THS& | Samuel | Cade | 1257 Corker St | Jacksonville, TX 75074|" ] */ 

Try this 尝试这个

long_string.match(/\S.+?(?=( THS&)|$)/g)

Space before THS is for trimming last space THS之前的空间用于修剪最后一个空间

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

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