I have this string:
const inputString = 'Crew: CAP(X): Ches, Ben, FO: Smith, Matthew, FO(T,P): Singh, Sushant';
I want to split this string so that the output is like so:
[
'Crew',
'CAP(X): Ches, Ben, ',
'FO: Smith, Matthew, ',
'FO(T,P): Singh, Sushant'
]
So split but take the text immediately before.
I tried this:
const result = inputString.split(/(?<=: )/);
console.log(result);
2
Answers
You can split it by space, then loop over it,
if contains
:
, push a new item, otherwise just concatenate it.Something like below,
This is a tricky one due to "Crew" not being the same as the rest of the string, plus all the additional commas and colons. Here’s my attempt: