简体   繁体   中英

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.

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&

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. The solution is to use a look-ahead , which does not actually capture the string:

 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. 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)

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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