I have string content that gets delivered to me via TCP. This info is only relevant because it means that I do not consistently retrieve the same string. I have a <start>
and <stop>
separator to ensure that any time I get the data via TCP, I am outputting the full content.
My incoming content looks like so:
<start>Apple Bandana Cadillac<stop>
I want to get everything in between <start>
and <stop>
. So just Apple Bandana Cadillac
.
My script to do this looks like so:
servercsv.on("connection", function(socket){
let d_basic = "";
socket.on('data', function(data){
d_basic += data.toString();
let d_csvindex = d_basic.indexOf('<stop>');
while (d_csvindex > -1){
try {
let strang = d_basic.substring(0, d_csvindex);
let dyson = strang.replace(/<start>/g, '');
let dson = papaparse.parse(dyson);
myfunction(dson);
}
catch(e){ console.log(e); }
d_basic = d_basic.substring(d_csvindex+1);
d_csvindex = d_basic.indexOf('<stop>');
}
});
});
What this means is that I am getting everything before the <stop>
string and outputting it. I have also included the line let dyson = strang.replace(/<start>/g, '');
because I want to remove the <start>
text.
However, because this is TCP, I am not guranteed to get all parts of this string. As a result, I frequently get back stop>Apple Bandana Cadillac<stop>
or some variation of this (such as start>Apple Bandana Cadillac<stop>
. It is not consistent enough that I can just do strang.replace("start>", "")
Ideally, I would like my separator to select content that is in between <start>
and <stop>
. Not just <stop>
. However, I am unsure how to do so.
Alternatively, I can also settle for a regex that retrieves all combination of <start><stop>
strings during my while loop, and just delete them. So check for <, s, t, a, r, t
individually and so forth. But unsure how to implement regex to delete portions of a whole string.